我在使用Intent.ACTION_OPEN_DOCUMENT创建、删除、移动、复制文件时遇到了问题。

huangapple 未分类评论47阅读模式
英文:

I am facing issues with creating, deleting, moving, coping files using Intent.ACTION_OPEN_DOCUMENT

问题

以下是翻译好的代码部分:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        intentToGetContentcode -> {
            if (resultCode == Activity.RESULT_OK) {
                val uri = data!!.data!!
                // ...permissions grantUriPermissions
                grantUriPermission(
                    "com.velabs.rerinavi2",
                    uri,
                    Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                )
                grantUriPermission(
                    "com.velabs.rerinavi2",
                    uri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                )
                grantUriPermission(
                    "com.velabs.rerinavi2",
                    uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION
                )
                val resolver = applicationContext.contentResolver
                val cursor = this.contentResolver.query(uri, null, null, null, null)
                resolver.takePersistableUriPermission(
                    uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                )
                val type = resolver.getType(uri)
                l("MIME type of .db", type!!)
                try {
                    val path = getDatabasePath("renamedName(1).db").absolutePath
                    l("renamedName(1) path", path)
                } catch (e: Exception) {
                    l("documentFile", "error $e,,,,${DocumentFile.fromSingleUri(this, uri)!!.isFile.toString()}")
                }
                try {
                    val doc = DocumentsContract.renameDocument(resolver, uri, "renamedName.db")
                } catch (e: Exception) {
                    l("rename", e.toString())
                }
                try {
                    val file = resolver.openInputStream(uri)
                    // val homeFile = getDatabasePath("export.db").outputStream()
                    // file!!.copyTo(homeFile)
                } catch (e: Exception) {
                    l("copyTo", e.toString())
                }
            }
        }
    }
    super.onActivityResult(requestCode, resultCode, data)
}

private fun exportToLocation() {
    if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
        checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
    ) {
        requestPermissions(
            arrayOf(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            ),
            requestPermissionCode
        )
    } else {
        val intentToGetContent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intentToGetContent.addCategory(Intent.CATEGORY_OPENABLE)
        intentToGetContent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        intentToGetContent.type = "application/octet-stream"
        startActivityForResult(
            Intent.createChooser(intentToGetContent, "Import file"),
            intentToGetContentcode
        )
    }
}

请注意,代码的清晰度可能因为翻译和复制而有所降低,如果您在实际使用中遇到问题,可能需要根据原始代码进行调整。

英文:

When I select file to manipulate from internal storage using Intent.ACTION_DOCUMENT_OPEN, logcat logs an error described below.

java.lang.SecurityException: Permission Denial: reading com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FrenamedName%20(1).db from pid=5361, uid=10398 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            intentToGetContentcode -> {
                if (resultCode == Activity.RESULT_OK) {
                    val uri = data!!.data!!
                    // ...permissions grantUriPermissions
                    grantUriPermission(
                        "com.velabs.rerinavi2",
                        uri,
                        Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                    )
                    grantUriPermission(
                        "com.velabs.rerinavi2",
                        uri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                    )
                    grantUriPermission(
                        "com.velabs.rerinavi2",
                        uri,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION
                    )
                    val resolver = applicationContext.contentResolver
                    val cursor =
                        this.contentResolver.query(uri, null, null, null, null)
                    resolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION and  Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

//                    val path: Uri = FileProvider.getUriForFile(
//                        context,
//                        "com.velabs.rerinavi.fileprovider",
//                        filelocation
//                    )
                    val type = resolver.getType(uri)
                    l("MIME type of .db",type!!)
                    try {
                        val path = getDatabasePath("renamedName(1).db").absolutePath
                        l("renamedName(1) path",path)

                    }catch (e:Exception){
                        l("documentFile","error $e,,,,"+DocumentFile.fromSingleUri(this,uri)!!.isFile.toString())
                    }

                    try {
                        val doc = DocumentsContract.renameDocument(resolver, uri, "renamedName.db")
                    }catch (e:Exception) {
                        l("rename",e.toString())
                    }
                    try {
                        val file = resolver.openInputStream(uri)
//                        val homeFile = getDatabasePath("export.db").outputStream()
//                        file!!.copyTo(homeFile)
                    }catch (e:Exception) {
                        l("copyTo",e.toString())

                    }                   
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

(Apologies for the messy code, I am still learning)
An illustration of how to copy a file from internal storage to one's app's database directory would be extremely helpful.
Here's the intent I am using:

 private fun exportToLocation() {
        if(checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
            checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            requestPermissions(arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE),requestPermissionCode)
        }else {
            val intentToGetContent =
                Intent(Intent.ACTION_OPEN_DOCUMENT) //ACTION_GET_CONTENT replaced by opendocuments //action_open_doc works
            intentToGetContent.addCategory(Intent.CATEGORY_OPENABLE)
            intentToGetContent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            intentToGetContent.type =
                "application/octet-stream" //application/db doesn't work //application/octect-stream works
            // inference for above application is correct /(this part needs to be corrected)
            startActivityForResult(
                Intent.createChooser(intentToGetContent, "Import file"),
                intentToGetContentcode
            )
        }

huangapple
  • 本文由 发表于 2020年5月4日 20:43:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61592494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定