I have legacy code that calls Environment.getExternalStorageDirectory()
, which shouldn't be accessible in Android 11 with scoped storage.
path = String.format("%s/%s", Environment.getExternalStorageDirectory(), MY_SUBFOLDER);
File directory = new File(path);
I've updated my targetSdkVersion=30
, so scoped storage should prevent me from accessing the external storage directory. This appears to function as described on some devices, the Google Play Pre Launch report shows my app crashing on a Galaxy S20 and Pixel 5 running Android 11. It appears that directory
is null
in this case. This is expected, and should happen with my legacy code.
I have a Moto Edge Plus running Android 11, and a Pixel 5 running Android 12. My legacy code seems to run fine on these devices.
The question is, why am I able to open the directory on my physical devices?
The debugger shows me accessing the directory with no problem. I've also tried enabling the FORCE_ENABLE_SCOPED_STORAGE
App Compatibility flag, and everything still works.
How can I make the scoped storage allocation fail on my two physical devices? Are there some Android 11/12 devices that don't enable Scoped Storage? Is it disabled when connected to a debugger? I'm curious why I can't reproduce the Scoped Storage protections.