Reputation: 143
We are forced to implement SAF as an update to the app is rejected by Google Play. It's a terrible user and developer experience especially if you already have an app in field for years and need to support old smartphones as well.
We need to support min API 24.
minSdkVersion 24
Aim:
I want the user of the app to select a folder in shared storage where files are stored which should not be deleted on app uninstallation.
Background:
The problem arises from the fact that the Documents folder is not present on all smartphones; the APIs change and the existing app folder /appName cannot be used any more.
Complete path to the Documents folder may also vary depending on which mounted device they are created (i.e. /Documents is not reliable)
Approach:
I) API 24 - 28:
"Documents" folder is not present on smartphones with these APIs by default.
Shared storage access is not limited.
We can create the "Documents" by simply calling mkdirs()
getExternalStoragePublicDirectory() is used for accessing the Documents directory
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), appFolder);
II) API 30 +
shared storage access limited
"*Documents*" folder exists by default on smartphones with these APIs by default
User is asked to pick base folder. By default, we suggest and recommend "*Documents*" as standard base folder in our app documentation.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
III) API 29:
shared storage access is not limited
[getExternalStoragePublicDirectory() is deprecated][1] and cannot be used
Thus cannot navigate the user to "*Documents*" folder and call *mkdirs()*
Currently user sees a picker just like for above mentioned API 31+ case but not showing the root folder in the beginning and needs to create the Documents folder first
Possible solutions:
I'd prefer a consistent app behavior with similar user interactions on all phones. However, it seems to be impossible.
a. Document that the user needs to ceated the Documents folder first manually -> this is a terrible user experience b. Call mkdirs on hard-coded path "/Documents/appFolder" may not work on all phones with API 24 - 29.
Your suggestion:
Which solution a) or b) would you take?
Do you recommend any other approach for an user-friendly workflow which should support smartphones API 24+?
Upvotes: 0
Views: 1038
Reputation: 9292
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
You can use this location an all devices.
Some times you need mkdirs() and sometimes the function is not supported but then use getExternalStorageDirectory().
No need to use SAF or user interaction.
Upvotes: 0