Reputation: 73741
I am trying to backup my databases from my app to my sd card but every time I try I get FileNotFoundException
and I am not sure why when the name of the database is right
code to backup databases
try{
File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");
File exportDir = new File(Environment.getExternalStorageDirectory()+"/BCAData");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
file.createNewFile();
FileChannel inChannel = new FileInputStream(dbFile).getChannel(); //fails here
FileChannel outChannel = new FileOutputStream(file).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}catch(Exception e){
e.printStackTrace();
}
in my content provider I have the database name like this
private static final String DATABASE_NAME = "Games";
private static final String GAMES_TABLE = "Games";
error:
12-14 19:38:38.313: W/System.err(10452): java.io.FileNotFoundException: /data/data/com.tyczj.bowling/databases/Games.db: open failed: ENOENT (No such file or directory)
12-14 19:38:38.313: W/System.err(10452): at libcore.io.IoBridge.open(IoBridge.java:416)
12-14 19:38:38.313: W/System.err(10452): at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-14 19:38:38.323: W/System.err(10452): at com.tyczj.bowling.services.DatabaseExportService.onHandleIntent(DatabaseExportService.java:27)
12-14 19:38:38.323: W/System.err(10452): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-14 19:38:38.333: W/System.err(10452): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:38:38.333: W/System.err(10452): at android.os.Looper.loop(Looper.java:137)
12-14 19:38:38.343: W/System.err(10452): at android.os.HandlerThread.run(HandlerThread.java:60)
12-14 19:38:38.343: W/System.err(10452): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-14 19:38:38.353: W/System.err(10452): at libcore.io.Posix.open(Native Method)
12-14 19:38:38.353: W/System.err(10452): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-14 19:38:38.353: W/System.err(10452): at libcore.io.IoBridge.open(IoBridge.java:400)
12-14 19:38:38.353: W/System.err(10452): ... 6 more
I dont see why this is wrong that is the path so is that not the name of the database?
Upvotes: 1
Views: 1034
Reputation: 1006614
Instead of:
File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");
Try:
File dbFile = getDatabasePath(DATABASE_NAME);
Upvotes: 4