gpt4 book ai didi

java - 如何从我的 Android 应用程序的所有目录中获取所有 pdf 文件

转载 作者:行者123 更新时间:2023-12-05 00:15:28 24 4
gpt4 key购买 nike

我想知道如何从内部存储中获取所有 PDF 文件。文件可以在任何目录中,例如一些在 DCIM 文件夹中或一些在下载文件夹中,等等。

注意:我使用的是Android Studio(语言:Java)。

最佳答案

我搜索了几乎所有的答案,但它在 Android 11 中不起作用。所以我有一个简短的解决方案来获取所有文件,例如图像、视频、pdf、文档、音频等。有一个 github 库,它是最近创建

点击[这里] https://github.com/HBiSoft/PickiT

如果你想在没有依赖的情况下完成所有这些

下面的代码

  • 按顺序做

var mimeTypes = arrayOf(
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"application/pdf"
)

findViewById<Button>(R.id.btnclick).setOnClickListener {

val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);

// mimeTypes = mimeTypes
}

startActivityForResult(intent, 100)

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.e(">>>>>>", "check")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getFile(this,intentToDocumentFiles(data)[0])
}
}

private fun intentToDocumentFiles(intent: Intent?): List<DocumentFile> {
val uris = intent?.clipData?.run {
val list = mutableListOf<Uri>()
for (i in 0 until itemCount) {
list.add(getItemAt(i).uri)
}
list.takeIf { it.isNotEmpty() }
} ?: listOf(intent?.data ?: return emptyList())

return uris.mapNotNull { uri ->
if (uri.isDownloadsDocument && Build.VERSION.SDK_INT < 28 && uri.path?.startsWith("/document/raw:") == true) {
val fullPath = uri.path.orEmpty().substringAfterLast("/document/raw:")
DocumentFile.fromFile(File(fullPath))
} else {
fromSingleUri(uri)
}
}.filter { it.isFile }
}

@RequiresApi(Build.VERSION_CODES.R)
fun getFile(context: Context, document: DocumentFile): File? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return null
}
try {
val volumeList: List<StorageVolume> = context
.getSystemService(StorageManager::class.java)
.getStorageVolumes()
if (volumeList == null || volumeList.isEmpty()) {
return null
}

// There must be a better way to get the document segment
val documentId = DocumentsContract.getDocumentId(document.uri)
val documentSegment = documentId.substring(documentId.lastIndexOf(':') + 1)
for (volume in volumeList) {
val volumePath: String
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
val class_StorageVolume = Class.forName("android.os.storage.StorageVolume")
val method_getPath: Method = class_StorageVolume.getDeclaredMethod("getPath")
volumePath=method_getPath.invoke(volume).toString()
} else {
// API 30
volumePath=volume.directory!!.path
}
val storageFile = File(volumePath + File.separator + documentSegment)

// Should improve with other checks, because there is the
// remote possibility that a copy could exist in a different
// volume (SD-card) under a matching path structure and even
// same file name, (maybe a user's backup in the SD-card).
// Checking for the volume Uuid could be an option but
// as per the documentation the Uuid can be empty.
val isTarget = (storageFile.exists()
&& storageFile.lastModified() == document.lastModified()
&& storageFile.length() == document.length())
if (isTarget) {
Log.e(">>>>>>>",storageFile.absolutePath)
return storageFile
}
}
} catch (e: Exception) {
e.printStackTrace()
}
Log.e(">>>>>>>","null")
return null
}

关于java - 如何从我的 Android 应用程序的所有目录中获取所有 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66353871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com