1

I am testing my app on a device that runs Android 9. The app now targets Android 11 as per my Gradle file:

android {

compileSdkVersion 30
buildToolsVersion '30.0.3'

defaultConfig {
    applicationId "com.xxx.yyy.zzz"
    minSdkVersion 15
    targetSdkVersion 30
    versionCode 15455
    versionName "4.55"
    multiDexEnabled true

}

Before running the app, I deleted the previous version from the test device.

When the app targeting Android 11 is run with the following code:

   try {
        File oldDir = new File(Environment.getExternalStorageDirectory(), "MyFolder");
        File afile = new File(oldDir, "myFile.boo");
        afile.createNewFile();
    }
    catch(IOException e){}

The app creates MyFolder/myFile.boo which is accessible and deletable.

The developer docs clearly state: https://developer.android.com/about/versions/11/privacy

"Apps that target Android 11 or higher are always subject to scoped storage behaviors." The docs do not mention any exceptions for a particular phone's android build.

Why am I able to create and access this file under the new scoped storage restrictions when my app has declared that it will be using scoped storage?

user1608385
  • 609
  • 8
  • 17
  • `The app creates MyFolder/myFile.boo` He he createNewFile(); will not create folders. – blackapps Dec 20 '20 at 19:32
  • The MyFolder was created by the app previously. Nonetheless, I am able to create a new folder in Environment.getExternalStorageDirectory() as follows: File aFile = new File(Environment.getExternalStorageDirectory(), "your_file_name"); aFile.mkDirs(). The "your_file_name" folder is now created and visible to other apps on a device running Android 9 where the app targets android 11. – user1608385 Dec 20 '20 at 19:39
  • Of course on an Android 9 device. Your min SdkVersion is 15. The rest, the 30, is irrelevant. I thought you saw it on an Android 11 device. – blackapps Dec 20 '20 at 19:52
  • 1
    `when my app has declared that it will be using scoped storage?` Your app can only follow the possibilities of the device. Apps cannot declare that they use scoped storage. And scoped storage is no -extra- feature but a restriction. – blackapps Dec 20 '20 at 19:56
  • Thanks. If you amend your answer so that it includes something like "and scoped storage is only enforced on devices that run Android 11 for apps targeting android 11 or Android 10 without requestLegacyExternalStorage="true" declared in the app and agree with that, I will accept the answer. Usually when there are new restrictions on an android version, the docs will say "when running on a device running Android ? and up...". That this statement was missing threw me for a loop. – user1608385 Dec 20 '20 at 20:09
  • read this https://stackoverflow.com/a/66366102/9917404 – Thoriya Prahalad Feb 25 '21 at 09:50

1 Answers1

0

Yes, but I do not believe that you can create your own folders in root of external storage on an Android 11 device. But you can in all the normal public directories on external storage. You have to use mkdir() or mkdirs() though to create a folder.

Android 11 is less restrictive as advertised.

Accessing external storage in Android API 29

See point 3. of answer.

Lino
  • 5,084
  • 3
  • 21
  • 39
blackapps
  • 8,011
  • 2
  • 11
  • 25