Requesting and allowing WRITE_EXTERNAL_STORAGE permission at runtime has no effects on the current session Requesting and allowing WRITE_EXTERNAL_STORAGE permission at runtime has no effects on the current session android android

Requesting and allowing WRITE_EXTERNAL_STORAGE permission at runtime has no effects on the current session


http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE:

Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir().

Runtime permissions start at API level 23, obviously above 19, so the permission is not required anymore unless you're accessing the data outside of the folder pointed by getExternalFilesDir(). Therefore, I believe this is a bug only present when testing on an emulator.

On targets below level 19, which don't support requesting permission at runtime, just claim the permission in manifest and it will work.


I had the same problem. Turns out this seems to be a bigger issue. Changing the permission to write to the external storage changes the GID for this process (on the linux side).In order to change the ID the process has to be restarted. Next time you open the app, the new groupID is set and the permission is granted.

Long story short, I'm afraid this is not a bug in the emulator but in fact a bigger issue with Linux and Android.

I "solved" this by asking for permission the first time the app is executed and restarting it when the permission is given like this:

PackageManager packageManager = getPackageManager();                    Intent intent = packageManager.getLaunchIntentForPackage(getPackageName());                    ComponentName componentName = intent.getComponent();                    Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName);                    startActivity(mainIntent);                    System.exit(0);

You may try to create a service running in the background (having another process id) and giving it the permission. That way you would only need to restart the service and not the complete app. On the down side this might make more work for you.

Hope this helps.

--- EDIT ---

The user M66B (https://stackoverflow.com/a/32473449/1565635) found a list of the related gids. Further information can be found here: https://android.googlesource.com/platform/frameworks/base/+/master/data/etc/platform.xml


For Android 10+

In my case, it was:

If targeting Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's AndroidManifest file.Just add this below code in your Application block.

<application                android:requestLegacyExternalStorage="true"        ... >