1

I have this code, that should be executed, when SD card is removed from an Android device (by remove, I understand, that user selects remove from Android device menu or configuration, just as you remove USB device or SD card from any other system, before phisically removing the card):

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

Card should be uninstalled and removed, but it seems, that it is actually installed during this process.

What am I doing wrong?

trejder
  • 17,148
  • 27
  • 124
  • 216
tedris
  • 1,118
  • 2
  • 7
  • 11

2 Answers2

0

You can find further information in the Android documentation.

There is a nice post here talking about detecting mounted sd card via USB.

you can also use this useful method instead :

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;
}
Community
  • 1
  • 1
Mycoola
  • 1,135
  • 1
  • 8
  • 29
0

code when I remove the card is mounted, but it must be unmounted. I solved this problem with another code. running smoothly. no problem

    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();
        }
tedris
  • 1,118
  • 2
  • 7
  • 11