I take the result of my file chooer in onActivityResult
an build a DocumentUriUsingTree
.
Now I take this DocumentUri
, build a DocumentFile
to create a new folder.
onActivityResult:
if (resultCode == RESULT_OK) {
uri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
getContext().getContentResolver().takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
}
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri);
subfolderDf.createDirectory("TestFolder");
uriDirFolder = uri.toString();
editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putString(PREF_FILE_OBSERVER, uriDirFolder);
editor.apply();
}
resultUri == uriDirFolder get from SharedPreference
get-Methode
Uri docUriTree = DocumentsContract.buildDocumentUriUsingTree(Uri.parse(resultUri), DocumentsContract.getTreeDocumentId(Uri.parse(resultUri)));
docTreeUri = content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%20Card%2FMusic%....
DocumentFile df = DocumentFile.fromTreeUri(this, docUriTree);
DocumentFile dirDf = df.createDirectory(dirName);
dirDf looks like content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%....dirName
df = DocumentFile.fromTreeUri(this,dirDf.getUri());
df returns the parent folder and not the new created folder
I need the DocumentFileUri
from the new folder to use an OutputStream
on it for saving files into it.
What I'm doing wrong here?
Thanks, Alejandro
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri); subfolderDf.createDirectory("TestFolder");
Change to:
DocumentFile folder = DocumentFile.fromTreeUri(this, uri);
DocumentFile subfolder = folder.createDirectory("TestFolder");
if ( subfolder == null )
{
Toast.makeText(context, "Sorry, could not create a subfolder with name TestFolder", Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(context, "Created a subfolder with name TestFolder: " + subfolder.getUri().toString(), Toast.LENGTH_LONG).show();