将文本文件写入SD卡失败

Pau*_*aul 10 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();
    }
}   
Run Code Online (Sandbox Code Playgroud)

Ara*_*cem 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;
}
Run Code Online (Sandbox Code Playgroud)


Squ*_*onk 6

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

Environment.getExternalStorageState()
Run Code Online (Sandbox Code Playgroud)

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


Wah*_*Haq 5

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

干杯,

wahib