0

I am trying to read and write to external storage in Android 7. I am using the following code to check the read and write status.

public void checkStorage() {

    File sd = Environment.getExternalStorageDirectory();

    if (sd.canWrite() && sd.canRead())
        Toast.makeText(context, "sd can read and write", Toast.LENGTH_LONG).show();
    else if (sd.canWrite() && !sd.canRead())
        Toast.makeText(context, "sd cannot read but write", Toast.LENGTH_LONG).show();
    else if (!sd.canWrite() && sd.canRead())
        Toast.makeText(context, "sd can read but not write", Toast.LENGTH_LONG).show();
    else if (!sd.canWrite() && !sd.canRead())
        Toast.makeText(context, "sd cannot read and write", Toast.LENGTH_LONG).show();

}

I have also mentioned both the permissions in manifest file which are

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

but still I am getting that sd(external) storage is neither readable nor writable. Has anybody encountered such a problem? I am out of ideas how to solve the issue.

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
Dilazak
  • 139
  • 1
  • 1
  • 11

1 Answers1

1

Starting form android 6.0, you should also check for permissions in runtime along with using them in the manifest.

To handle runtime permissions I have created a helper library which may help you: https://github.com/nabinbhandari/Android-Permissions

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59