繁体   English   中英

我无法从 android 的下载文件夹中获取文档

[英]I can't fetch documents from the downloads folder in android

我正在尝试从下载文件夹中附加文档。 但是我收到错误“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:", "")
                                }
                            }
                        }
                    }
                }

正如异常所指出的,您的磁盘上没有这样的文件/目录,或者提供的路径不正确。

乍一看,您似乎错过了文件名和下载目录/storage/emulated/0/DownloadReact-native commands.pdf之间的斜线。 确保文件名和路径之间有斜线。

val path = Environment.getExternalStorageDirectory().toString() +"/Download/" + fileName
val ss = Environment.getExternalStoragePublicDirectory("").toString()+"/Download/" + fileName 

看看它是否有效。 否则,请验证您是否在相应目录中归档。

getExternalStorageDirectory在 API 级别 29 java 中已弃用。 使用getExternalFilesDir()getExternalCacheDir()getExternalMediaDir() (上下文方法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM