2012-10-18 133 views
1

我正在使用以下代碼來檢查Sd卡是否存在並且是Writable.But當我在模擬器的Sd卡上下文中使用該代碼時,它顯示Sd卡不存在於模擬器中,但實際上,文件資源管理器中顯示相應的Emulator.This的SD卡中的內容是代碼:如何有效檢查SD卡是否存在且可寫入?

static public boolean hasStorage(boolean requireWriteAccess) { 
    //TODO: After fix the bug, add "if (VERBOSE)" before logging errors. 
    String state = Environment.getExternalStorageState(); 
    System.out.println("storage state is " + state); 

    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     if (requireWriteAccess) { 
      boolean writable = checkFsWritable(); 
      System.out.println("storage writable is " + writable); 
      return writable; 
     } else { 
      return true; 
     } 
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     return true; 
    } 
    return false; 
} 

此代碼表明SD卡未安裝,但文件資源管理器中顯示不同picture.Please幫助我提前感謝。

+0

請幫助別人。 – user1726619

+0

我們正在閱讀該代碼。不要期望在2分鐘內回答。 –

+0

我可以等先生。 – user1726619

回答

2

這項工作將檢查兩個安裝,它是否可讀或不可讀。

private boolean isExternalStorageAvailable() { 

     String state = Environment.getExternalStorageState(); 
     boolean mExternalStorageAvailable = false; 
     boolean mExternalStorageWriteable = false; 

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

     if (mExternalStorageAvailable == true 
       && mExternalStorageWriteable == true) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
+0

在我的代碼中檢查SD卡是否掛載有問題嗎? – user1726619

+0

它似乎沒有,但我已經在仿真器和設備上面的代碼測試,運行良好。 –

+0

它也顯示了相同的結果,SD卡不存在,但實際上是。 – user1726619