如何检入代码是否只读外部存储设备

时间:2012-11-23 05:38:47

标签: android sd-card readonly

在我的Android应用程序中,我将一些文件写入SD卡。 但是,如果SD卡是READ-ONLY,则存在这样的情况 然后我必须相应地更改屏幕设置。 如果SD卡是只读的,我如何在我的代码中检查它?

2 个答案:

答案 0 :(得分:2)

尝试使用Environment.MEDIA_MOUNTED_READ_ONLY作为:

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();
}

答案 1 :(得分:2)

更全面的答案......

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;

}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
//    check_folder();
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;

}