Environment.getExternalStorageState()在具有可用内存的HTC上返回MEDIA_REMOVED

时间:2014-05-15 16:14:14

标签: android

在我的应用程序中,我试图检查用户是否已将手机连接到PC作为驱动器,因此我可以警告他们断开连接,因为我需要访问存储。

除了 HTC Desire X手机,Android 4.0.4 外,它在我测试过的所有4-5台设备上都能正常使用。它没有SD卡,但有大约2 GB的存储空间可供写入。

以下是我使用的代码

private void checkStorage() {
    // Get the external storage's state
    String state = Environment.getExternalStorageState();
    Log.d("STATE", state);
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        // Storage is available and writeable
        externalStorageAvailable = externalStorageWriteable = true;
    } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        // Storage is only readable
        externalStorageAvailable = true;
        externalStorageWriteable = false;
    } else {
        // Storage is neither readable nor writeable
        externalStorageAvailable = externalStorageWriteable = false;
    }
}

因此,当我运行它时,logcat调试标记返回:

removed

根据文件:

http://developer.android.com/reference/android/os/Environment.html#MEDIA_REMOVED

"Storage state if the media is not present."

如何修改此代码,以便检测是否存在可供使用的内存?

现在,还有另外一件事。在这款手机上,当我尝试从浏览器下载图像时,我不允许,我收到以下消息:

No SD card
An SD card is required to download <filename>
OK

手机有问题还是这是正常的?如何让我的应用在这款手机上运行?

编辑:此外,如果我能够解决这个问题,我该如何将文件写入存储?这是我的代码,可以在其他设备上运行:

File directory = null;
    File file = null;

    try {
        directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), 
                Utils.getWritableDiectory(Locale.ENGLISH));

        if (!directory.exists()) {
            directory.mkdirs();
        }

            file = new File(directory, "data.json");

0 个答案:

没有答案
相关问题