0

I want to get content (images & videos) of user selected folder. Code to select the folder is:

val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                    or Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
                    or Intent.FLAG_GRANT_READ_URI_PERMISSION
                    or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
        )
startActivityForResult(intent, 1088)

And onActivityResult, I am persisting the permission(even I tested without restarting the device but it is not working):

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == Activity.RESULT_OK) {
           if (requestCode == 1088) {
               val selectedDirUri = data!!.data

                grantUriPermission(packageName, selectedDirUri, (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                        or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION))

                val takeFlags = (data.flags
                        and (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        or Intent.FLAG_GRANT_WRITE_URI_PERMISSION))
                contentResolver.takePersistableUriPermission(selectedDirUri!!, takeFlags)
             }
        }
   }

And I am traversing through all files using:

val rootUri: Uri = Uri.parse(selectedDir)

            val contentResolver: ContentResolver = context.contentResolver
            var childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri))

            val dirNodes: MutableList<Uri> = LinkedList()
            dirNodes.add(childrenUri)

            while (!dirNodes.isEmpty()) {
                childrenUri = dirNodes.removeAt(0) // get the item from top
                
                val c = contentResolver.query(childrenUri,
                    arrayOf(
                        DocumentsContract.Document.COLUMN_DOCUMENT_ID,
                        DocumentsContract.Document.COLUMN_DISPLAY_NAME,
                        DocumentsContract.Document.COLUMN_MIME_TYPE,
                        DocumentsContract.Document.COLUMN_LAST_MODIFIED),
                    null, null, null)
                try {
                    while (c!!.moveToNext()) {
                        val docId = c.getString(0)
                        val fileName = c.getString(1)
                        val mimeType = c.getString(2)
                        val lastModified = c.getLong(3)
                        
                        if (isDirectory(mimeType)) {
                            

                            val newNode = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId)
                            dirNodes.add(newNode)
                        } else {
                            

                            val newNode = DocumentsContract.buildDocumentUriUsingTree(rootUri, docId)
                            Logger.log("TAG", "========1 Images: $newNode")
                            
                            val ***sourceFileUri*** = newNode.toString()
                        }
                    }
                } finally {
                    closeQuietly(c)
                }
            }

Then I display images and videos using Glide, it is not displaying. Even if I try to copy the image using below code:

val inputStream: InputStream? = context.contentResolver.openInputStream(***sourceFileUri***)

I am getting below error, the above line gives an error:

java.lang.SecurityException: com.android.externalstorage has no access to content://media/external _primary/file/1000008384 at android.os.Parcel.createException or Null(Parcel.java:2438) at 08 android.os.Parcel.createException(P arcel.java:2422) at android.os.Parcel.readException(Par cel.java:2405) at android.database.DatabaseUtils.rea dExceptionFromParcel(DatabaseUtil s.java:190) at android.database.DatabaseUtils.rea dException WithFileNotFoundExcepti on From Parcel(DatabaseUtils.java:15 3)

This is happening in Vivo, Oppo and specially in new Samsung phones only which has android OS 11 and 12. I am really frustrated, I tried all possible solution but not able to find any solution till now.

Any solution or advice would be really helpful and appreciated, please please help me.

18
  • Please remove all flags from OPEN_DOCUMENT_TREE intent. They do nothing. Commented Nov 9, 2022 at 7:09
  • Please remove the call to grantUriPermission() as it does nothing. Commented Nov 9, 2022 at 7:10
  • You did not show how you obtained the sourceFileUris. Commented Nov 9, 2022 at 7:13
  • @blackapps I added that line in my question. And regarding flags and grantUriPermission(), if I remove, will it work or it is just not necessary as you mentioned that is does nothing? Please check updated question. Commented Nov 9, 2022 at 7:57
  • A childuri is no sourceFileUri. So we know nothing more yet. And you are not showing how you use Glide. Commented Nov 9, 2022 at 8:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.