复制的 android db 文件为空
我正在尝试将 android db 文件从我的应用程序文件夹复制到 SD 卡上的另一个文件夹。从 DDMS 文件资源管理器中我可以注意到复制的文件大小为 0。这是我的代码如下。
public boolean copyDBFile(){
File dbFile =new File(Environment.getDataDirectory() + DB_PATH);
File exportDir = new File(Environment.getExternalStorageDirectory()
+ "/BACKUP_DIR");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
copyFile(dbFile, file);
return true;
} catch (IOException e) {
return false;
}
}
public 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();
}
}
是权限问题吗?感谢您的帮助。
I'm trying to copy an android db file from my application folder to another one on SD card. And from DDMS file explorer i can notice that the copied file size is 0. This is my code bellow.
public boolean copyDBFile(){
File dbFile =new File(Environment.getDataDirectory() + DB_PATH);
File exportDir = new File(Environment.getExternalStorageDirectory()
+ "/BACKUP_DIR");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
copyFile(dbFile, file);
return true;
} catch (IOException e) {
return false;
}
}
public 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();
}
}
Is it a permission issue? Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@piyush
感谢您的尝试/捕获通知。在 catch 块的 boolean copyDBFile() 方法中添加日志跟踪后,我发现了错误。
我的
DB_PATH
已设置为
/data/data/package/databases/data.db
并添加到Environment.getDataDirectory()
code>dbFile 结果到/data/data/data/package/databases/data.db
这是一个大错误!感谢大家的帮助:)
@piyush
Thanks for the try/catch notice. I found the error after adding a log trace in
boolean copyDBFile()
method at catch block.}
My
DB_PATH
was already set to/data/data/package/databases/data.db
and added toEnvironment.getDataDirectory()
thedbFile
result to/data/data/data/package/databases/data.db
That's the big mistake! Thanks all for help :)
在使用
transferTo(...);
之前尝试使用outChannel.force(true);
try using
outChannel.force(true);
before using thetransferTo(...);