[英]I can't fetch documents from the downloads folder in android
I am trying to attach documents from download folder.我正在尝试从下载文件夹中附加文档。 But i am getting the error "java.io.FileNotFoundException: /storage/emulated/0/DownloadReact-native commands.pdf (No such file or directory)".Here i show the code that i used to get path.但是我收到错误“java.io.FileNotFoundException:/storage/emulated/0/DownloadReact-native commands.pdf(没有这样的文件或目录)”。这里我展示了我用来获取路径的代码。
if (isDownloadsDocument(uri)) {
Log.d("build_version_one", Build.VERSION.SDK_INT.toString())
Log.d("build_version_two", Build.VERSION_CODES.M.toString())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val id: String
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, arrayOf(MediaStore.MediaColumns.DISPLAY_NAME), null, null, null)
if (cursor != null && cursor.moveToFirst()) {
val fileName = cursor.getString(0)
// val path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName
val path = Environment.getExternalStorageDirectory().toString() +"/Download" + fileName
val ss = Environment.getExternalStoragePublicDirectory("").toString()+"/Download" + fileName
Log.d("orginal_path","one"+path)
Log.d("orginal_path","two"+ss)
if (!TextUtils.isEmpty(path)) {
Log.d("orginal_path","working")
return path
}
}
} finally {
cursor?.close()
}
id = DocumentsContract.getDocumentId(uri)
if (!TextUtils.isEmpty(id)) {
if (id.startsWith("raw:")) {
return id.replaceFirst("raw:".toRegex(), "")
}
val contentUriPrefixesToTry = arrayOf(
"content://downloads/public_downloads",
"content://downloads/my_downloads"
)
for (contentUriPrefix in contentUriPrefixesToTry) {
return try {
val contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), java.lang.Long.valueOf(id))
Log.d("orginal_content_uri", contentUri.toString())
/* final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));*/
getDataColumn(context, contentUri, null, null)
} catch (e: NumberFormatException) {
//In Android 8 and Android P the id is not a number
uri.path.replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "")
}
}
}
}
}
As the Exception states, there is no such file/directory available on your disk or path provided is incorrect.正如异常所指出的,您的磁盘上没有这样的文件/目录,或者提供的路径不正确。
At the first glance, it seems you missed a slash between filename and Download directory /storage/emulated/0/DownloadReact-native commands.pdf
.乍一看,您似乎错过了文件名和下载目录/storage/emulated/0/DownloadReact-native commands.pdf
之间的斜线。 Make sure you have a slash between filename and a path.确保文件名和路径之间有斜线。
val path = Environment.getExternalStorageDirectory().toString() +"/Download/" + fileName
val ss = Environment.getExternalStoragePublicDirectory("").toString()+"/Download/" + fileName
See if it works.看看它是否有效。 Otherwise verify if you file in the respective directory.否则,请验证您是否在相应目录中归档。
getExternalStorageDirectory
deprecated in API level 29 java. getExternalStorageDirectory
在 API 级别 29 java 中已弃用。 Use getExternalFilesDir()
, getExternalCacheDir()
, or getExternalMediaDir()
(methods on Context).使用getExternalFilesDir()
、 getExternalCacheDir()
或getExternalMediaDir()
(上下文方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.