Android SD卡检测错误

时间:2014-12-24 16:35:23

标签: android android-sdcard

我有这个代码,应该执行,当从Android设备中删除SD卡时(通过删除,我知道,该用户从Android设备菜单或配置中选择remove,就像删除USB设备一样或者在任何其他系统中使用SD卡,然后才能从卡上移除卡片):

public void kartkontrol()
{
    String state = android.os.Environment.getExternalStorageState();
    if (state.equals(android.os.Environment.MEDIA_MOUNTED))
    {
        // We can read and write the media
        Durum=" Hafıza Kartı Var    (Okuma/Yazma)";

    }
    else if (android.os.Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
    {
        // We can only read the media
        Durum=" Hafıza Kartı Var     (Okuma İzni)";
    }
    else
    {
        // No external media

        Durum=" Hafıza Kartı Yok";
    }
}

卡应该被卸载并删除,但似乎它实际上是在此过程中安装的。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以在Android documentation

中找到更多信息

这里有一个很好的post,谈论通过USB检测已安装的SD卡。

您也可以使用这种有用的方法:

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

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            Log.v(TAG, "storage writable is " + writable);
            return writable;
        } else {
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

答案 1 :(得分:0)

我卸下卡时的

代码已安装,但必须卸载。我用另一个代码解决了这个问题。运行顺利。没问题

    sSDpath = null;
        fileCur = null;


for (String sPathCur : Arrays.asList("ext_card", "external_sd", "ext_sd", "external","extSdCard", "externalSdCard")) // external sdcard
        {
            fileCur = new File("/mnt/", sPathCur);
            if (fileCur.isDirectory() && fileCur.canWrite()) {
                sSDpath = fileCur.getAbsolutePath();

                kartk.setText("Hafıza Kartı Takılı");
                Toast.makeText(getApplicationContext(), "kkkBirinci", Toast.LENGTH_LONG).show();
                break;
            }
        }
        fileCur = null;
        if (sSDpath == null) {

            sSDpath = Environment.getExternalStorageDirectory().getAbsolutePath();

            kartk.setText("Hafıza Kartı Takılı Değil");
            Toast.makeText(getApplicationContext(), "kkkikinci", Toast.LENGTH_LONG).show();
        }