Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

android - Writing Text File to SD Card fails

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 why.

For some people, it force closes if they put some characters like ... in the File and such. I cannot seem to reproduce it as I've had no troubles but this is the code which handles the File writing. Can anyone think of something that may lead to problems or a better to way to do it?

public void generateNoteOnSD(String sFileName, String sBody)
{
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) 
        {
            root.mkdirs();
        }

        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();

        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
         importError = e.getMessage();
         iError();
    }
}   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
this 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.
The 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...