android-如何访问pdf页的属性?

qqrboqgw  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(271)

我需要访问打印机应用程序的pdf页的属性。
这些属性是:;
页面大小和宽度
页边(请看照片)

如何以编程方式获取这些属性?
注意:我没有放任何代码示例,因为我现在只有一个“file”类型的pdf文件。

7jmck4yq

7jmck4yq1#

根据文件(https://developer.android.com/training/data-storage/shared/documents-files#kotlin),当您拥有文档的uri时,就可以访问其元数据。此代码段获取uri指定的文档的元数据,并将其记录下来

val contentResolver = applicationContext.contentResolver

fun dumpImageMetaData(uri: Uri) {

    // The query, because it only applies to a single document, returns only
    // one row. There's no need to filter, sort, or select fields,
    // because we want all fields for one document.
    val cursor: Cursor? = contentResolver.query(
            uri, null, null, null, null, null)

    cursor?.use {
        // moveToFirst() returns false if the cursor has 0 rows. Very handy for
        // "if there's anything to look at, look at it" conditionals.
        if (it.moveToFirst()) {

            // Note it's called "Display Name". This is
            // provider-specific, and might not necessarily be the file name.
            val displayName: String =
                    it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
            Log.i(TAG, "Display Name: $displayName")

            val sizeIndex: Int = it.getColumnIndex(OpenableColumns.SIZE)
            // If the size is unknown, the value stored is null. But because an
            // int can't be null, the behavior is implementation-specific,
            // and unpredictable. So as
            // a rule, check if it's null before assigning to an int. This will
            // happen often: The storage API allows for remote files, whose
            // size might not be locally known.
            val size: String = if (!it.isNull(sizeIndex)) {
                // Technically the column stores an int, but cursor.getString()
                // will do the conversion automatically.
                it.getString(sizeIndex)
            } else {
                "Unknown"
            }
            Log.i(TAG, "Size: $size")
        }
    }
}

相关问题