Question

I'm doing a little app that inserts people and then if i want export it to CSV. I was able to do that but the file it's exporting to the sd card of the emulator and i wanted it to export do the internal storage (Downloads or another place). I already searched here to see if i could find an answer but nothing i found resolved my problem.

File dbFile = getDatabasePath("androidituts");
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
    exportDir.mkdirs();
}
File file = new File(exportDir, "teste.csv");
try {
    file.createNewFile();
    CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
    Cursor curCSV = mydb.rawQuery("SELECT * FROM test", null);
    csvWrite.writeNext(curCSV.getColumnNames());
    while (curCSV.moveToNext()) {
        String arrStr[] = {curCSV.getString(1)};
        csvWrite.writeNext(arrStr);
    }
    csvWrite.close();
    curCSV.close();
} catch (Exception sqlEx) {
    Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}

Does anyone know can i change the file path?? I think that it's in the part Environment.getExternalStorageDirectory, but anytime i change that to anything else it says:

E/MainActivity(1519): open failed: ENOENT (No such file or directory)
 E/MainActivity(1519): java.io.IOException: open failed: ENOENT (No such file or directory)
Was it helpful?

Solution

Hope this will help

 try {
    File exportDir = new File(myDir + "/text/", filename);
    if (exportDir .getParentFile().mkdirs()) {
        exportDir .createNewFile();
        FileOutputStream fos = new FileOutputStream(exportDir );

        fos.write(outputString.getBytes());
        fos.flush();
        fos.close();
    }
} catch (Exception e) {
    e.printStackTrace();
}

OTHER TIPS

replace

    File exportDir = new File(Environment.getExternalStorageDirectory(), "");

to

    File exportDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top