I am having trouble finding a .csv file I am creating and writing to in the file explorer once I've connected my Android device to my computer.
I have the WRITE_EXTERNAL_STORAGE
in my manifest like so:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am writing to the file using this function in my main activity:
public void exportDatabase() {
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ "/Folder");
boolean var = false;
if (!folder.exists()) {
Log.d(EXPORT_DATA_TAG, "Folder does not exist");
var = folder.mkdir();
Log.d(EXPORT_DATA_TAG, "mkdir() success? " + var);
}
CharSequence contentTitle = getString(R.string.app_name);
final ProgressDialog progDailog = ProgressDialog.show(
this, contentTitle, "Exporting data...",
true);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
final String filename = folder.toString() + "/" + "LeadCapture.csv";
Log.d(EXPORT_DATA_TAG,"Writing data to file: " + filename);
new Thread() {
public void run() {
boolean success = writeData(filename);
if (success) {
handler.sendEmptyMessage(0);
Log.d(EXPORT_DATA_TAG,"Write successful to .csv");
} else {
Log.d(EXPORT_DATA_TAG,"Failed to write to .csv");
}
progDailog.dismiss();
}
}.start();
}
This calls a write function in my database helper class:
public boolean writeData(String filename){
leadDatabase = openOrCreateDatabase(database_name, MODE_PRIVATE, null);
try {
FileWriter fw = new FileWriter(filename);
Cursor cursor = leadDatabase.rawQuery("select * from " + database_name, null);
fw.append("Index");
fw.append(',');
fw.append("BarcodeData");
fw.append(',');
fw.append("Notes");
fw.append(',');
fw.append('\n');
if (cursor.moveToFirst()) {
do {
fw.append(cursor.getString(0));
fw.append(',');
fw.append(cursor.getString(1));
fw.append(',');
fw.append(cursor.getString(2));
fw.append(',');
fw.append('\n');
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
fw.close();
leadDatabase.close();
return true;
} catch (Exception e) {
Log.d(DATABASE_TAG,"Error: ",e);
leadDatabase.close();
return false;
}
}
The Logcat says that the file is successfully being written to /storage/sdcard1/Download/Folder/LeadCapture.csv
, but I can't find the folder or the file in the Downloads folder or anywhere.
I have tried changing
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
to
File folder = new File(Environment.getExternalStorageDirectory())
This results in this path /storage/sdcard1/Folder/LeadCapture.csv
but I am still unable to find the file there. I;m very confused because in most of the other questions about this problem, it has been an issue with not having the WRITE_EXTERNAL_STORAGE
permission, them not knowing the filepath, or them writing to Internal memory, none of which is the case here. In theory, even if my writeFunction() isn't correct, it should at least be creating an empty directory that I can see. Any ideas?