4

In my app I am downloading some files from Internet. I am giving choice where to store them, internal storage or external sd card. Till Kitkat, I could write without problems. In Kitkat it was blocked unless app is system app or phone is rooted, I am trying to make it work with Lollipop & Marsmallow.

I have such permissions:

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

Test device is Samsung Galaxy S5 with 6.0.1 and external SD card.

I retrieve list of storages by reading "/proc/mounts". It returns to me "storage/emulated/0" and "storage/7074-XXXX"

For Marshmallow, I also check the access like that:

private static final int REQUEST_EXTERNAL_STORAGE = 200;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};



public static void VerifyStoragePermissions(Activity activity) {
    if(android.os.Build.VERSION.SDK_INT >= 23) {
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
    }
}

onRequestPermissionsResult procedure tells me that I have write access. It does not show a confirmation screen etc.

If I select "storage/emulated/0" I can write to this directory, but not the second. I receive error "open failed: EACCES (Permission denied)"

What I want is mainly encourage users to write those files to external SD card if it is present. Please help.

Floern
  • 33,559
  • 24
  • 104
  • 119
Ozkan Tuzemen
  • 155
  • 11
  • Possible duplicate of: http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card –  Jul 12 '16 at 09:12
  • try this http://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 – Sohail Zahid Jul 12 '16 at 09:12
  • I am using actually ActivityCompat.checkSelfPermission, in onRequestPermissionsResult I see that I have permission, in one folder I can write in another I can't. – Ozkan Tuzemen Jul 12 '16 at 09:18
  • `I am giving choice where to store them, internal storage or external sd card`. You forgot external storage. – greenapps Jul 12 '16 at 11:06
  • 1
    You are only able to write to micro SD card in the app specific directory if there is. `pathtosdcard/Android/data/`. Have a look at `GetExternalFilesDirs()`. – greenapps Jul 12 '16 at 11:07
  • @greenapps, what I am listing all possible storages to user. But for me first priority should be external SD card, files are over a gigabyte. – Ozkan Tuzemen Jul 12 '16 at 11:19
  • @greenapps I already use GetExternalFilesDir() as safety option, if user cannot write onto SD card they have this option, but it is not external SD card, it is storage/emulated/0 in my device's case. – Ozkan Tuzemen Jul 12 '16 at 11:22
  • `I already use GetExternalFilesDir() `. I did not say that. I said: `GetExternalFilesDirs()`. `Dirs!` Try the second one if it returns more entries. – greenapps Jul 12 '16 at 11:25
  • I tested that. It returns to me 2 folder addresses that I can write but they are not the folder names which I would like to write. Problem is, my app already has over 180 000 users, if I change storage directory, I'll have to write separate code for moving those files. I wouldn't like that. What I want is to be able to write the root dir of the SD card. Somehow system allowes me write to "/storage/emulated/0" but not SD card. – Ozkan Tuzemen Jul 12 '16 at 12:21
  • Have you got an resolution? I met the same issue. All replies on stackoverflow are using ` ActivityCompat.requestPermissions` which do not work for SD card. – Leon Oct 15 '17 at 04:44

0 Answers0