0

I have added the following permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Application can create any file or directory under the filesDir (See below code). But it can't create any custom directory in the "/storage/emulated/0/".

TestActivity.java

public void test(Context context) {

    File filesDir = context.getExternalFilesDir(null);
    System.out.println("filesDir: " + filesDir.getAbsolutePath());

    File subFilesDir = new File(filesDir, "SubFilesDir");
    boolean subFilesDirCreated = subFilesDir.mkdirs();
    System.out.println("subFilesDir: " + subFilesDirCreated + ", exists: " + subFilesDir.exists());

    File testDir = new File("/storage/emulated/0/TestDir");
    boolean testDirCreated = testDir.mkdirs();
    System.out.println("testDir: " + testDirCreated + ", exists: " + testDir.exists());

}

Here is the output: (The testDir can't be created!)

I/System.out: filesDir: /storage/emulated/0/Android/data/com.testapp/files
I/System.out: subFilesDir: true, exists: true
I/System.out: testDir: false, exists: false

It shouldn't relate to the "root" permission because it is just "/storage/emulated/0/". All applications should be able to create any custom file or dir in that directory.

What's the problem? How to fix?

Thanks.

stackbiz
  • 1,136
  • 1
  • 5
  • 22
  • 1
    "All applications should be able to create any custom file or dir in that directory" -- not on Android 10 or 11 by default. – CommonsWare Jun 13 '20 at 17:26
  • Hi @CommonsWare, after adding android:requestLegacyExternalStorage="true" to the tag, now application can read files from /storage/emulated/0/TestDir, but it still can't create any subdir in /storage/emulated/0/TestDir. This attribute only allows reading existing directory but not allowing create addition directory dynamically. Please help to check, Thanks again. – stackbiz Jun 14 '20 at 06:50
  • Make sure that you requested both `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` at runtime. Your question only shows the manifest entries. – CommonsWare Jun 14 '20 at 11:07

0 Answers0