Writing Text File to SD Card fails

You can use this method to check de sdCard state. Change the toast dialog to you language:

** Care with MEDIA_MOUNTED_READ_ONLY. In no need write in the SDCard and i return true **

public static Boolean comprobarSDCard(Context mContext) {
    String auxSDCardStatus = Environment.getExternalStorageState();

    if (auxSDCardStatus.equals(Environment.MEDIA_MOUNTED))
        return true;
    else if (auxSDCardStatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        Toast.makeText(
                mContext,
                "Warning, the SDCard it's only in read mode.\nthis does not result in malfunction"
                        + " of the read aplication", Toast.LENGTH_LONG)
                .show();
        return true;
    } else if (auxSDCardStatus.equals(Environment.MEDIA_NOFS)) {
        Toast.makeText(
                mContext,
                "Error, the SDCard can be used, it has not a corret format or "
                        + "is not formated.", Toast.LENGTH_LONG)
                .show();
        return false;
    } else if (auxSDCardStatus.equals(Environment.MEDIA_REMOVED)) {
        Toast.makeText(
                mContext,
                "Error, the SDCard is not found, to use the reader you need "
                        + "insert a SDCard on the device.",
                Toast.LENGTH_LONG).show();
        return false;
    } else if (auxSDCardStatus.equals(Environment.MEDIA_SHARED)) {
        Toast.makeText(
                mContext,
                "Error, the SDCard is not mounted beacuse is using "
                        + "connected by USB. Plug out and try again.",
                Toast.LENGTH_LONG).show();
        return false;
    } else if (auxSDCardStatus.equals(Environment.MEDIA_UNMOUNTABLE)) {
        Toast.makeText(
                mContext,
                "Error, the SDCard cant be mounted.\nThe may be happend when the SDCard is corrupted "
                        + "or crashed.", Toast.LENGTH_LONG).show();
        return false;
    } else if (auxSDCardStatus.equals(Environment.MEDIA_UNMOUNTED)) {
        Toast.makeText(
                mContext,
                "Error, the SDCArd is on the device but is not mounted."
                        + "Mount it before use the app.",
                Toast.LENGTH_LONG).show();
        return false;
    }

    return true;
}

Leave a Comment