Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing external storage in Android API 29

I'm trying to achieve some clean up tools. More and more manufacturers have forbidden rooting devices due to some "security reason", it's forbidden NOT to request for unlock.

After API 28, This code will make error:


ActivityCompat.requestPermissions(this, new String[]{
  Manifest.permission.READ_EXTERNAL_STORAGE,
  Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1); // Request permission or not, Will got same result

File rootFolder = Environment.getExternalStorageDirectory(); // That is working fine
rootFolder.listFiles(); // That will return null

Sure, I can use this:

android:requestLegacyExternalStorage="true"

But I belive that will be killed in future.

So, Any elegant way to manage SDCard?

like image 260
Alceatraz Avatar asked Sep 11 '20 06:09

Alceatraz


People also ask

How do you access storage on Android?

You can also access free internal storage from your Android phone through Settings > System > Storage > Device storage. Here you can preview what data are using your internal storage and how much free storage you can use furtherly.

How do I access external files on Android 11?

Users can see which apps have the READ_EXTERNAL_STORAGE permission in system settings. On the Settings > Privacy > Permission manager > Files and media page, each app that has the permission is listed under Allowed for all files. If your app targets Android 11, keep in mind that this access to "all files" is read-only.


1 Answers

On Android 10 Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() will return storage paths but paths are not readable or writable.

  1. For Android 10 you can continue to use paths provided by Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() if you add android:requestLegacyExternalStorage="true" to application tag in manifest file. At runtime your app can call Environment.isExternalStorageLegacy() to check if the request has been done.

  2. Another (not known) possibility (only for Android 10) is to add <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> to manifest file. The user has to go to the advanced settings of the app and enable from Advanced settings Install unknown apps | Allow from this source. The nice thing with this is that the user can switch the access rights. You can make it easier for the user if you implement an intent for Settings.ACTION_APPLICATION_DETAILS_SETTINGS where he can change the settings. A funny thing is that Environment.isExternalStorageLegacy() returns true then too.

  3. Compiling for Android 11 both options do not work on an Android 11 device. (But they continue to work for Android 10 devices). The paths of Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() are usable again in read mode and very often in write mode too. And this is great as one can simply list the contents of directories like Download or Pictures or DCIM/Camera again using the File class. But adding <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> to manifest file and implementing an intent for Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION will give your app read/write access for all files even on removable micro sd card. (Finally you can remove the google ban not being able to read/write your own micro sd card on your own Android device using your own app). Environment.isExternalStorageManager() can be used to check if the permission is on/off. As long as you do not try to upload your app to the play store you are fine.

like image 200
blackapps Avatar answered Sep 21 '22 15:09

blackapps