Android 复制文件不适用于本地 SQLite 文件
我正在使用这段代码将文件从内部存储复制到外部存储,而不会丢失任何内容:
public static void copyFile(String currentDBPath, String backupDBPath) {
try {
File sd = Environment.getExternalStorageDirectory();
//File data = Environment.getDataDirectory();
if (sd.canWrite()) {
File currentDB = new File(currentDBPath);
File backupDB = new File(backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
当我尝试从内部存储或 txt 文件复制图像时,它工作正常,没有任何错误,并且新文件位于 SD 卡中。但是,如果我尝试从 /data/data/package/databases/system.sqlite
复制本地数据库文件,它会在给定的目标中创建一个新文件,但新的数据库文件是空的,没有任何内容表格甚至数据。
知道为什么它只发生在数据库文件上吗?
编辑:
正如我注意到的,复制的新数据库文件与原始数据库文件一样大(145KB),但是当我用sqlite浏览器打开它时,没有表,没有数据...什么都没有。
I'm using this piece of code to copy files from internal to external storage without losing any :
public static void copyFile(String currentDBPath, String backupDBPath) {
try {
File sd = Environment.getExternalStorageDirectory();
//File data = Environment.getDataDirectory();
if (sd.canWrite()) {
File currentDB = new File(currentDBPath);
File backupDB = new File(backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
When I try to copy images from internal storage or txt files it's working fine, without any errors and the new files are in sd card. But if I try to copy my local database files from /data/data/package/databases/system.sqlite
it's creating a new file in the given destination, but the new database file is empty, without any tables or even data on it.
Any ideas why it's happening only with database files?
Edit:
As I noticed, the new database file which is copied is as big as the original one (145KB), but when I open it with sqlite browser there are no tables, no data...nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我们使用的:
我们能够将数据库备份到 SD 卡并使用 SQLite 浏览器将其读回。
This is what we use:
We're able to back up the database to the sdcard and read it back using SQLite Browser.