Android 尝试将数据库备份到SD卡时FileNoFoundException
我正在尝试将我的应用程序中的数据库备份到sd卡,但每次尝试时,我都会得到Android 尝试将数据库备份到SD卡时FileNoFoundException,android,Android,我正在尝试将我的应用程序中的数据库备份到sd卡,但每次尝试时,我都会得到FileNotFoundException,我不知道为什么数据库的名称正确 备份数据库的代码 try{ File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db"); File exportDir = new File(Environment.getExter
FileNotFoundException
,我不知道为什么数据库的名称正确
备份数据库的代码
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();
}
在我的内容提供商中,我的数据库名称如下
private static final String DATABASE_NAME = "Games";
private static final String GAMES_TABLE = "Games";
错误:
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
12-14 19:38:38.313:W/System.err(10452):java.io.FileNotFoundException:/data/data/com.tyczj.bowling/databases/Games.db:open failed:enoint(没有这样的文件或目录)
12-1419:38:38.313:W/System.err(10452):位于libcore.io.IoBridge.open(IoBridge.java:416)
12-14 19:38:38.313:W/System.err(10452):位于java.io.FileInputStream。(FileInputStream.java:78)
12-14 19:38:38.323:W/System.err(10452):位于com.tyczj.bowling.services.DatabaseExportService.onhandleint(DatabaseExportService.java:27)
12-14 19:38:38.323:W/System.err(10452):位于android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-14 19:38:38.333:W/System.err(10452):在android.os.Handler.dispatchMessage(Handler.java:99)上
12-14 19:38:38.333:W/System.err(10452):在android.os.Looper.loop(Looper.java:137)
12-14 19:38:38.343:W/System.err(10452):在android.os.HandlerThread.run(HandlerThread.java:60)上
12-14 19:38:38.343:W/System.err(10452):由以下原因引起:libcore.io.ErrnoException:open failed:enoint(没有这样的文件或目录)
12-14 19:38:38.353:W/System.err(10452):位于libcore.io.Posix.open(本机方法)
12-14 19:38:38.353:W/System.err(10452):位于libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-14 19:38:38.353:W/System.err(10452):位于libcore.io.IoBridge.open(IoBridge.java:400)
12-1419:38:38.353:W/系统错误(10452):。。。还有6个
我不明白为什么这是错误的,那是路径,那不是数据库的名称吗?而不是:
File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");
尝试:
而不是:
File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");
尝试:
你比我快了四秒钟,但是仔细看看
数据库名称
这里没有“.db”
@Sam:啊,说得好。我已经调整了我的答案以匹配。OP:没有自动文件扩展名添加到数据库中,因此无论您使用SQLiteOpenHelper
或SQLiteDatabase
作为名称,都是文件名。您仍然应该使用getDatabasePath()
,但是,要获取文件
对象。啊,我明白了,我认为它需要.db
,因为它是一个数据库文件,所以去掉了它,效果非常好,谢谢!你比我快了四秒钟,但是仔细看看数据库名称
这里没有“.db”
@Sam:啊,说得好。我已经调整了我的答案以匹配。OP:没有自动文件扩展名添加到数据库中,因此无论您使用SQLiteOpenHelper
或SQLiteDatabase
作为名称,都是文件名。您仍然应该使用getDatabasePath()
,但是,要获取文件
对象。啊,我明白了,我认为它需要.db
,因为它是一个数据库文件,所以去掉了它,效果非常好,谢谢!