java - Android - 如何访问 PDF 页面的属性?
问题描述
解决方案
根据文档(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")
}
}
}
推荐阅读
- angular - 无法使用 localhost 通过 Microsoft Edge 浏览器连接到 SignalR 服务器
- android - 无法在初始屏幕中显示富媒体消息
- c# - 如何从名称不包含 0 的目录中获取所有文件?
- python - 当 Accuracy 已经是 1.0 时停止在 Keras 中训练
- html - 为什么模式与我的输入不正确匹配?
- angularjs - 使用具有公共范围的指令作为隔离范围
- sapui5 - 一开始就限制空间
- android - adb 服务器版本 (39) 与此客户端 (40) 不匹配;
- angular - 如何创建对动态创建的组件的引用?
- c# - 使用 Quartz.NET 和 Autofac 的每个 JobExecutionContext 的实例