0

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.

dragonx
  • 14,963
  • 27
  • 44
  • `report shows my app crashing`. Catch the exception that lets your app crash and your app will not crash anymore. And should we guess which code you use that gives you a problem? How to ask a question? – blackapps Mar 16 '22 at 08:23
  • Pretty unclear what your actual problem is. – blackapps Mar 16 '22 at 08:25
  • I rewrote the question. I'm trying to reproduce the problem on my own devices. It's not the crash in particular - I know the file creation is failing and hence I get a null pointer exception that I can catch. But this doesn't happen on my test devices. – dragonx Mar 16 '22 at 16:41
  • You should thoroughly test your legacy code on multiple Android 11 and 12 devices. I have a legacy app which is reported crashed 1.4K times on those devices: Vivo V2026, Samsung Galaxy A51, realme C11 2021, Coolpad 1904. – ישו אוהב אותך Mar 16 '22 at 17:17

0 Answers0