10

我遇到了一个奇怪的问题。我的应用程序可以将一个简单的文本文件写入 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();
    }
}   
4

5 回答 5

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;
}
于 2011-02-23T09:18:12.153 回答
6

您是否正在检查外部存储是否可写?如果没有,请尝试使用...

Environment.getExternalStorageState()

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

于 2011-01-02T22:27:10.813 回答
5

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

干杯,

瓦希卜

于 2012-06-17T22:07:41.483 回答
3

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

这在所有手机/ROM 版本上都不正确。CMod7 和 MIUI ROMS 都允许您设置插入 PC 时是否安装 SD 卡,根据您的设置,以上可能适用。最好检查一下。

于 2011-05-22T20:47:19.780 回答
0

我通常使用PrintWriter而不是 FileWriter。我不知道它是否能解决您的问题,但它的级别更高,因此它可能比简单的 FileWriter 处理更多的事情。

于 2011-01-02T22:49:38.313 回答