0

I got treeUri from intent's resultdata. I selected sdcard's root path. Casting treeUri to string type's result is content://com.android.externalstorage.documents/tree/C4FD-B5C6%3A.

Question 1.

Is that result right?

Question 2.

Anyway, I created Documentfile like this.

DocumentFile tree = DocumentFile.fromTreeUri(MusicServiceActivity.getAppContext(),Uri.parse(stringtreeUri));.

And I printed

tree.canWrite

using Log.e, but it always returns false.

How can I make this to return true?

Added

Here's my onActivityResult..

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode != RESULT_OK)
        return;
    else {
        Uri treeUri = resultData.getData();
        Log.e("treeuri",treeUri.toString());
        grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        if(Build.VERSION.SDK_INT >= 19)
            getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        String str = treeUri.toString();
        try{
            File dir = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
            if(!dir.exists()){
                dir.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/treeuri.txt", true);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
            writer.write(str);
            writer.flush();
            writer.close();
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

1 Answers1

0

Is that result right?

It looks reasonable.

How can I make this to return true?

I am interpreting your code as:

  1. You got a valid document tree Uri (e.g., via ACTION_OPEN_DOCUMENT_TREE)
  2. You converted the Uri to a string
  3. You saved the string somewhere (database, SharedPreferences, etc.)
  4. Later, you are trying the code that you have (converting the string back to a Uri, then trying to use that Uri)

If so, then after step #1, you need to call takePersistableUriPermission() on a ContentResolver. Otherwise, only the activity that got the Uri originally has rights to the content identified by that Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @YooMinjun: That should allow you to use that `Uri` again in the future, assuming that there have not been major changes (e.g., that document tree no longer exists). – CommonsWare Jan 29 '19 at 14:33
  • Ummm... I'm using that, but it stil shows false on tree.canWrite()... @CommonsWare – Yoo Minjun Jan 29 '19 at 15:33
  • @YooMinjun: I have never tried `canWrite()` on a tree `Uri`. Try logging the `canWrite()` result after your `takePersistableUriPermission()` call. If that too logs `false`, then perhaps `canWrite()` is only for documents, not trees. If so, try creating a document `Uri` in that tree and see if you can write to it. – CommonsWare Jan 29 '19 at 16:04