0

I found this method to export db to CSV but when it creates a file it says permission denied I have already granted permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Here is the code

private void exportDB() {

    File dbFile = getDatabasePath("db_journal.db");
    db = new JournalDB(getApplicationContext());
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");
    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, "ojt.csv");
    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        SQLiteDatabase sql = db.getReadableDatabase();
        Cursor curCSV = sql.rawQuery("SELECT * FROM logs", null);
        csvWrite.writeNext(curCSV.getColumnNames());
        while (curCSV.moveToNext()) {

            String arrStr[] = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2),curCSV.getString(3), curCSV.getString(4), curCSV.getString(5), curCSV.getString(6)};
            csvWrite.writeNext(arrStr);
        }
        csvWrite.close();
        curCSV.close();
    } catch (Exception sqlEx) {
        Log.e("ProfileMain", sqlEx.getMessage(), sqlEx);
    }
}

0 Answers0