将文本文件写入SD卡失败

时间:2011-01-02 21:46:12

标签: android android-sdcard android-file

我遇到一个奇怪的问题。我的应用程序可以写一个简单的文本文件到SD卡,有时它适用于一些人,但不适用于其他人,我不知道为什么。

对于某些人来说,如果他们在文件中添加...之类的字符等强制关闭。我似乎无法重现它,因为我没有麻烦,但这是处理文件写入的代码。任何人都可以想到可能导致问题的事情或更好的方法吗?

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();
    }
}   

5 个答案:

答案 0 :(得分:15)

您可以使用此方法检查de sdCard状态。将toast对话框更改为您的语言:

**关注MEDIA_MOUNTED_READ_ONLY。无需在SDCard中写入并返回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;
}

答案 1 :(得分:6)

您是否检查外部存储器是否可写?如果没有,那么尝试使用...

Environment.getExternalStorageState()

这将告诉您SD卡是否已安装,您还可以检查它是否可写。这就是我能想到的所有建议。

答案 2 :(得分:5)

我刚发现在我的情况下,我错过了在Manifest文件中添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

干杯,

wahib

答案 3 :(得分:3)

对于那些通过USB在实际设备上进行调试的新手:不要忘记从开发电脑上拔下USB电缆,就像我一样。连接USB时,SD卡不可用。快乐的文件写作......

这在所有手机/ ROM版本上都不正确。 CMod7和MIUI ROMS都允许您设置插入PC时SD卡是否已安装,具体取决于您的设置,上述情况可能适用。最好检查。

答案 4 :(得分:0)

我通常使用PrintWriter而不是FileWriter。我不知道它是否会解决你的问题,但它的级别更高,所以它可能会比简单的FileWriter更多地处理。

相关问题