0

I'm trying to export some SQLite table data. I'm getting the IOException: /TestFileExport0719a.csv: open failed: EROFS (Read-only file system)

Below is my code:

 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;

The devices are not rooted. Any help would be appreciated.

Thanks

I get the following error after I made the change to:

File 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: open failed: EACCES (Permission denied)

Edit-

Adding the permission in the manifest fixed the problem.

Thanks for the help.

1
  • Adding the permission fixed the problem. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Commented Jul 21, 2013 at 20:54

1 Answer 1

1

In the future, please post the entire stack trace.

Assuming that outFileName is TestFileExport0719a.csv, that is an invalid path. You need to write to either your portion of internal storage (e.g., getFilesDir()) or external storage (e.g., getExternalFilesDir()).

Since this is an export operation, I would assume you want to write to external storage, so the user can access the CSV file independently. In that case, change:

File outFile = new File(outFileName);

to:

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

or:

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

or something like that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.