After migrating to Android Q I can no longer find a suitable way to gain write access to the documents folder (/storage/emulated/0/Documents
)
And before anyone mentions the many other questions on scoped storage I've read through many of them and from what I've seen so far all of the solutions use a app specific directory or accessing a media directory only (not documents folder access).
From what I understand in Android Q I can either choose:
ACTION_OPEN_DOCUMENT_TREE
The applications I develop are used to conduct tests on subjects, the data from the tests automatically gets stored in the publicly accessible Documents folder somewhat like: /storage/emulated/0/Documents/myAppName/subjectName/testData-todaysDate.pdf
When the user wants to access the test data they plug their smartphone into a computer and navigate to the documents folder and the rest is pretty obvious. For this reason I have to use publicly accessible storage. Also imagine they want to open the test data on their phone via another app, Same deal!
So the solution I'm looking for needs to be able to do the following:
/storage/emulated/0/Documents/
ACTION_OPEN_DOCUMENT_TREE
I get that these changes in Android Q are to bring users more "into the loop" with how apps are accessing their data but once they understand how your app is using your data there shouldn't be a problem.
Stay organized with collections Save and categorize content based on your preferences. Scoped storage limits app access to external storage. In Android 11 or higher, apps targeting API 30 or higher must use scoped storage.
You need to provide the WRITE_EXTERNAL_STORAGE permission.
Opt out in your production app If your app targets Android 10 (API level 29) or lower, you can temporarily opt out of scoped storage in your production app. If you target Android 10, however, you need to set the value of requestLegacyExternalStorage to true in your app's manifest file: <manifest ... >
For Android Q only.
String collection = "content://media/1c11-2404/file"; // One can even write to micro SD card
String relative_path = "Documents/MyFolder";
Uri collectionUri = Uri.parse(collection);
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.SIZE, filesize);
contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, modified );
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relative_path);
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1)
Uri fileUri = context.getContentResolver().insert(collectionUri, contentValues);
Having an uri one can open an output stream to write the content for the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With