0

我正在尝试导出一些 SQLite 表数据。我收到 IOException: /TestFileExport0719a.csv: open failed: EROFS (Read-only file system)

下面是我的代码:

 Boolean returnCode = false;
        int i = 0;
        String csvValues = "";

        dbadapter = new DBAdapter(this);
        dbadapter.open();
        try {
            File outFile = new File(outFileName);
            FileWriter fileWriter = new FileWriter(outFile);
            BufferedWriter out = new BufferedWriter(fileWriter);
            Cursor cursor = dbadapter.getAllSkus();
            if (cursor != null) {
                   while (cursor.moveToNext()) {

                     csvValues = Long.toString(cursor.getLong(0)) + ",";
                        csvValues += cursor.getString(1)
                                + ",";
                        csvValues +=  cursor.getString(2)
                                + "\n";

                            out.write(csvValues);

                    }
                cursor.close();
            }
            out.close();
            returnCode = true;
        } catch (IOException e) {
            returnCode = false;
            Log.d(TAG, "IOException: " + e.getMessage());
        }
        dbadapter.close();
        return returnCode;

设备未植根。任何帮助,将不胜感激。

谢谢

更改为后出现以下错误:

文件 outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), outFileName);

07-20 18:31:07.595:D/BackupCSV(1580):IOException:/storage/emulated/0/Download/TestFileExport0719a.csv:打开失败:EACCES(权限被拒绝)

编辑-

在清单中添加权限解决了这个问题。

谢谢您的帮助。

4

1 回答 1

1

将来,请发布整个堆栈跟踪。

假设outFileNameTestFileExport0719a.csv,那是一个无效的路径。您需要写入您的内部存储部分(例如getFilesDir())或外部存储部分(例如getExternalFilesDir())。

由于这是一个导出操作,我假设您要写入外部存储,因此用户可以独立访问 CSV 文件。在这种情况下,更改:

File outFile = new File(outFileName);

至:

File outFile = new File(getExternalFilesDir(), outFileName);

或者:

File outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), outFileName);

或类似的东西。

于 2013-07-20T22:10:07.437 回答