Writing Text File to SD Card fails

后端 未结 5 687
长发绾君心
长发绾君心 2020-11-30 09:07

I have a strange problem I\'ve come across. My app can write a simple textfile to the SD Card and sometimes it works for some people but not for others and I have no idea wh

相关标签:
5条回答
  • 2020-11-30 09:59

    Are you checking that the external storage is writeable? If not then try using...

    Environment.getExternalStorageState()
    

    This will tell you if the SD card is mounted and you can also check if it's writeable. That's all I can think of to suggest at this point.

    0 讨论(0)
  • 2020-11-30 10:00

    I usually use PrintWriter rather than FileWriter. I don't know if it would solve your issue but it's of a higher level so it may take care of more things than a simple FileWriter.

    0 讨论(0)
  • 2020-11-30 10:04

    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;
    }
    
    0 讨论(0)
  • 2020-11-30 10:04

    I just found out that in my case, i was missing adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in Manifest file.

    Cheers,

    wahib

    0 讨论(0)
  • 2020-11-30 10:10

    For all fellow newbies out there debugging on an actual device via USB: Don't forget to unplug the USB cable from your dev PC, like I did. When USB is connected the SD card is unavailable. Happy file writing...

    This is not correct on all phones/ROM builds. CMod7 and MIUI ROMS both allow you to set whether the SD card is mounted or not when plugged into PC, depending on your settings the above may hold true. Best to check.

    0 讨论(0)
提交回复
热议问题