简体   繁体   English

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

[英]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.

相关问题 如何访问Android设备中的downloads / data文件夹? - How can I access downloads/data folder in Android device? 如何将文件下载到我的下载文件夹android - How can I download a file to my downloads folder android 在下载/文档文件夹中为文件类型/扩展名注册 Android 应用程序 - Registering Android App for File Types/Extensions in Downloads/Documents folder 我不明白为什么在 Android 11 中使用 `storage\emulated...\downloads` 文件夹可以读取/写入某些文件而不是其他文件? - I don't understand why in Android 11 use of `storage\emulated...\downloads` folder can read/write some files and not others? 无法从 Android 上的下载文件夹中读取文件 - Cannot read a file from Downloads folder on Android 无法将文本文件写入 Android 中的文档文件夹 - Can't write text files to documents folder in Android 无法在“下载”文件夹中打开已下载的mp3文件(Android Studio) - Downloaded mp3 files can't open in Downloads folder (Android Studio) Android 12:当应用上传到 Google Play 时,无法在下载中创建新文件夹 - Android 12: can't create new folder in downloads when app is uploaded to Google Play Android:如果我想从下载文件夹中查看/选择 SQLite 数据库,要使用的 mime 类型是什么? - Android: what is the mime type to use if I want to see/pick a SQLite database from the Downloads folder? Android无法从php获取数据 - Android can't fetch data from php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM