【问题标题】:how to check in code whether an external storage device is read only如何在代码中检查外部存储设备是否为只读
【发布时间】:2012-11-23 05:38:47
【问题描述】:

在我的 android 应用程序中,我将一些文件写入 SD 卡。 但是有一个条件,如果 SD 卡是只读的 然后我必须相应地更改屏幕设置。 如果 SD 卡是只读的,我如何在我的代码中检查它?

【问题讨论】:

    标签: android sd-card readonly


    【解决方案1】:

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

    【讨论】:

      【解决方案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;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-08
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 2015-12-06
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        相关资源
        最近更新 更多