androidkotlinstorage-access-frameworkdocumentfile

Can't convert Uri to String and String to Uri again


I am working on a music player and want to use Storage access framework to access the files in the storage. For that I used Intent.ACTION_OPEN_DOCUMENT_TREE and contentResolver.takePersistableUriPermission(...). But once I've got the permissions, I have to store the allowed path so I am using SharedPreferences for that.

When I convert the URI I got from the Intent.ACTION_OPEN_DOCUMENT_TREE to string to reuse it, it gives me a nullPointerException(It says the URI I got from converting the string from preferences is null).

While if I use the same URI without going through saving to the SharedPreferences, It works just fine.

Here's the code:

package com.gaurav712.music

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.documentfile.provider.DocumentFile

class MainActivity : AppCompatActivity() {

    private val openDocumentTreeRequestCode: Int = 40
    private lateinit var setPreferences: SharedPreferences
    private lateinit var rootUri: Uri

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setPreferences = getPreferences(Context.MODE_PRIVATE)

        // Check if access to storage is permitted
        checkAccess()

        // Now that we have access to storage, set the root Uri
        rootUri = Uri.parse(setPreferences.getString("allowed_path", ""))
        Log.i("rootUri", rootUri.toString())

//        listFiles()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == openDocumentTreeRequestCode && resultCode == RESULT_OK) {

            val treeUri = data?.data    // get data

            if (treeUri != null) {
                contentResolver.takePersistableUriPermission(treeUri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            }

            val editor = setPreferences.edit()
            editor.putBoolean("got_permissions", true)
            editor.putString("allowed_path", treeUri?.toString())
            editor.apply()

            Log.i("treeUri", treeUri.toString())

            val documentFile = treeUri?.let { DocumentFile.fromTreeUri(this, it) }

            for (file in documentFile?.listFiles()!!) {
                file.name?.let { Log.i("file: ", it) }
            }
        }
    }

    private fun checkAccess() {
        val gotPermission: Boolean = setPreferences.getBoolean("got_permissions", false)
        if (!gotPermission)
            getAccess()
        else
            Log.i("got_permissions",
                gotPermission.toString()
                        + " : "
                        + setPreferences.getString("allowed_path", ""))
    }

    private fun getAccess() {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
        startActivityForResult(intent, openDocumentTreeRequestCode)
    }

    private fun listFiles() {

        val documentFile: DocumentFile = DocumentFile.fromTreeUri(this, rootUri)!!

        for (file in documentFile.listFiles()) {
            Log.i("file: ", file.name.toString())
        }
    }
}

In onCreate() if I uncomment listFiles(), It gives nullPointerException but using the same chunk of code as you can see above in onActivityResult(), it all works fine. The chunk I am talking about:

            val documentFile = treeUri?.let { DocumentFile.fromTreeUri(this, it) }

            for (file in documentFile?.listFiles()!!) {
                file.name?.let { Log.i("file: ", it) }
            }

I can't figure out why it says rootUri is null.

I looked at all the similar questions asked like this one. I am using the same functions suggested (toString() and Uri.parse) for conversion but it doesn't seem to work with Storage access framework.


Solution

  • I solved it! the function getAccess() should be:

        private fun getAccess() {
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
                addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
            }
            startActivityForResult(intent, openDocumentTreeRequestCode)
        }
    

    I was missing Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION flag in the intent.