I am trying to create a database through my android application, which is working on an emulator.
However, as I cant view the database directly on my Samsung device (it is not rooted) I am trying to transfer the database file to the SD card.
The file is not successfully transferring to the SD card, when I try and open it in eclipse it is not there.
I am getting the following error in Logcat:
07-15 14:31:07.035: E/mypck(17369): /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): java.io.FileNotFoundException: /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.IoBridge.open(IoBridge.java:409)
07-15 14:31:07.035: E/mypck(17369): at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.copyFile(ExportDatabaseFileTask.java:71)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:49)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:1)
07-15 14:31:07.035: E/mypck(17369): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-15 14:31:07.035: E/mypck(17369): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-15 14:31:07.035: E/mypck(17369): at java.lang.Thread.run(Thread.java:841)
07-15 14:31:07.035: E/mypck(17369): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.Posix.open(Native Method)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.IoBridge.open(IoBridge.java:393)
07-15 14:31:07.035: E/mypck(17369): ... 10 more
Related Code:
Class relating to exporting the file:
public class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
//Default constructor
public ExportDatabaseFileTask() {
}
//delete if necessary
//private final ProgressDialog dialog = new ProgressDialog(null);
// can use UI thread here
protected void onPreExecute() {
// this.dialog.setMessage("Exporting database...");
// this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {
//original database file location
File dbFile = new File(Environment.getDataDirectory()
+ "/com.example.multapply/databases/MultapplyDatabase.db");
//the destination file location
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
this.copyFile(dbFile, file);
return true;
} catch (IOException e) {
Log.e("mypck", e.getMessage(), e);
return false;
}
}
// can use UI thread here
protected void onPostExecute(final Boolean success) {
// if (this.dialog.isShowing()) {
// this.dialog.dismiss();
// }
// if (success) {
// Toast.makeText( null, "Export successful!", Toast.LENGTH_SHORT)
// .show();
// } else {
// Toast.makeText(null, "Export failed", Toast.LENGTH_SHORT).show();
// }
}
void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
Code where this class is instantiated and called:
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));
//attempting to export the file to the sd card
ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();
Note: I was previously getting than error relating to "Writing exception to parcel" that I asked about here, but this error has gone now that I removed:
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
from the manifest.