【问题标题】:External Storage issue in androidandroid中的外部存储问题
【发布时间】:2013-04-03 05:42:13
【问题描述】:

我使用"/mnt/sdcard/myfolder/myfile" 的路径将我的文件存储在android 应用程序中。当应用程序在没有任何 sdcard 插槽的手机(像 Nexus S 的手机)上运行时会报错吗?

【问题讨论】:

  • 使用 /sdcard/myfolder/myfile 中的位置,/mnt 不是必需的。要检查您可以使用的 sdcard 的可用性,android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
  • 另请注意,您不应使用硬编码路径来访问外部存储。如果你还没有,你应该使用 Environment.getExternalStorageDirectory()。

标签: android android-sdcard


【解决方案1】:

您必须检查外部存储是否可用

private static boolean isExternalStorageAvailable() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        return true;
    }
    return false;
}

Demo

【讨论】:

    【解决方案2】:

    请查看link

    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    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;
    } 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 2023-02-19
      • 2012-03-30
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      相关资源
      最近更新 更多