Android: ACTION_CREATE_DOCUMENT initial folder /DCIM or /Documents rather than Download

225 Views Asked by At

since SDK33 my APP cannot access folders such as DCIM or Documents anymore after requesting permission.WRITE_EXTERNAL_STORAGE due to access not granted - as it was necessary while using older SDKs. A possible workaround was given in the subsequent cited Reddit thread by using the SAF instead:

Accessing Shared Storage on SDK 33, Android >= 11 from native API : r/androiddev (reddit.com)

I can confirm that after the user interacted with the filesystem via SAF by storing a file inside a particular folder later on this particular folder remains available to store other files an internal process of the app without further user interaction. This would solve my issue if I could let the user directly see a folder like DCIM or Documents instead of Downloads on their first interaction with the SAF right after installing this app. However so far I’ve researched other threads here

Pick a directory/file using Intent.ACTION_CREATE_DOCUMENT (Storage Access Framework) Pick a directory/file using Intent.ACTION_CREATE_DOCUMENT (Storage Access Framework)

and there

Cannot set the initial location of documents navigator using Storage Access Framework (SAF) Cannot set the initial location of documents navigator using Storage Access Framework (SAF)

those threads seem to suggest this is not possible. I also tried the following code which is invoked by UI (e.g.in button1onclick) however it also points always into the download folder no matter what is provided as initial uri after installing the app for the first time:

 String path = Environment.getExternalStorageDirectory()   + "/" + "DCIM" + "/";
Uri loc_pickerInitialUri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/jpg");
intent.putExtra(Intent.EXTRA_TITLE, "TestImage.jpg");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, loc_pickerInitialUri);
startActivityForResult( intent, 0);

I would really appreciate a hint on how I can make the document provider called by the code above is initially pointing to DCIM or Documents instead of Downloads to save a file after the app was installed on a (new) phone.

Edit: I rewrote my post to match recommendations given in comments below.

Edit1: Rewrote my post again to match results of the meanwhile gained knowledge – my initial issue an SDK33-compiled app’s request for permission.WRITE_EXTERNAL_STORAGE yields access not granted can easily be solved by just removing that request. This works for Android 11 ..13 for accessing the DCIM folder for creating and opening files and for creating subdirectories – hope this is correct so far and might also helps those who come to here due to the mentioned Reddit link above.

2

There are 2 best solutions below

5
On

Without SAF: On an Android 13 device your app can create files in all public directories on external storage without needing any permission.

So also in DCIM and Documents.

5
On

With SAF ACTION_CREATE_DOCUMENT: Try to manupilate initial uri like i did here:

Android 11 ACTION_OPEN_DOCUMENT_TREE: set initial URI to the Documents folder