Search code examples
androidsd-cardreadonly

how to check in code whether an external storage device is read only


In my android app I write some files to SD card. However there is a condition that if the SD card is READ-ONLY then I have to change screen settings accordingly. How can I check it in my code if a SD card is read only ?


Solution

  • try using Environment.MEDIA_MOUNTED_READ_ONLY as:

    String status = Environment.getExternalStorageState();
    if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        Toast.makeText(Activity.this, "SD MEDIA_MOUNTED", Toast.LENGTH_LONG).show();
    
    } else if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        Toast.makeText(Activity.this, "MEDIA_MOUNTED_READ_ONLY", 
              Toast.LENGTH_LONG).show();
    }