不同的ExternalStorageState状态

时间:2013-11-20 05:56:15

标签: android

说明在哪种情况下(例如:手机作为媒体连接到电脑,未安装SD卡),将满足以下条件。

if (Environment.MEDIA_MOUNTED.equals(state)) {
        // Can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // Can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Can't read or write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }   

1 个答案:

答案 0 :(得分:2)

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

这将检查外部存储器是否可用于读写。 getExternalStorageState()方法返回您可能要检查的其他状态,例如媒体是否正在共享(连接到计算机),是否完全丢失,是否已被严重删除等等。您可以使用这些状态通知用户在您的应用程序需要访问媒体时获得更多信息。

<强> Environment.MEDIA_MOUNTED

Check Storage state if the media is present and mounted at its mount point with read/write access. 
It will be true if the SD card is available.

<强> Environment.MEDIA_MOUNTED_READ_ONLY

It will true if sdcard is available and its Read only.