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();
}
}
}