范围存储:重新安装后无法删除由我自己的应用创建的文件

问题描述

我正在尝试使用以下代码读取通过我的应用下载的PDF文件

    @JvmStatic
    @RequiresApi(Build.VERSION_CODES.Q)
    fun readFileFromTheStorage(fileName: String,volumePath: Uri?,context: Context): Uri? {

        val collection = volumePath
                ?: MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)

        val projection = arrayOf(
                MediaStore.Downloads._ID
        )

        val selection = "${MediaStore.Downloads.disPLAY_NAME} = '" + fileName.replace("\'","_") + "'"

        context.contentResolver.query(
                collection,projection,selection,null,null
        )?.use { cursor ->
            while (cursor.movetoNext()) {
                val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID)
                val id = cursor.getLong(idColumn)
                val uri = ContentUris.withAppendedId(
                        collection,id)
                return uri
            }
        }

        return null
    }

如果我下载文件并将其存储在下载目录中,则可以读取该文件,但是如果我卸载该应用程序并再次重新安装,则上述代码将返回null。我不确定为什么会这样。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)