0

In Android I declare this permission and request the permission, user also grant this permission.

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

Then I have a such method to get the folder which I can use to write to.

    <string name="root_folder" translatable="false">MyRootFolder</string>

    public static File getFolder(Context ctx) {
    File folder;

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
        folder = Environment.getExternalStoragePublicDirectory(ctx
                .getString(R.string.root_folder));
    } else {
        folder = new File(ctx.getFilesDir(), ctx.getString(R.string.root_folder));
    }

    if (!folder.exists()) {
        folder.mkdir();
    }

    return folder;
}

For most devices, this is working fine, But on some devices, I found the folder doesn't exist, so I cannot save the app's output to user's device.

folder.exists() is false, folder.canWrite() is false.

What i want to do is find a folder where I can save the data to user's device, if the External storage is not available, i can use cache folder by context.getFilesDir()

John
  • 697
  • 1
  • 10
  • 33
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Nov 02 '17 at 14:20
  • `.getString(R.string.root_folder)` Should we know what you are asking for? – greenapps Nov 02 '17 at 14:36
  • `folder.mkdir();` Check the return value! `if ( !folder.mkdir()) return null;`. And be aware that `getFolder()` can return null now. Check that too before you continue. – greenapps Nov 02 '17 at 14:38
  • `if the External storage is not available, ` External storage is always available now adays. – greenapps Nov 02 '17 at 14:40
  • `folder = Environment.getExternalStoragePublicDirectory(ctx .getString(R.string.root_folder));` That will always fail as there is no public directory with the name `MyRootFolder`. What is the value of `folder.getAbsolutePath()` if you do so? – greenapps Nov 02 '17 at 14:42
  • @greenapps it will create the folder if not exists... – John Nov 02 '17 at 21:04

0 Answers0