gpt4 book ai didi

java - 复制的 android db 文件为空

转载 作者:行者123 更新时间:2023-11-30 11:51:56 24 4
gpt4 key购买 nike

我正在尝试将一个 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();
}
}

这是权限问题吗?感谢您的帮助。

最佳答案

@piyush感谢您的尝试/捕获通知。我在 catch block 的 boolean copyDBFile() 方法中添加日志跟踪后发现错误。

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) {
Log.e("Sarelo", "Error creating file", e);
return false;
}

我的 DB_PATH 已经设置为 /data/data/package/databases/data.db 并添加到 Environment.getDataDirectory() dbFile 结果到 /data/data/data/package/databases/data.db那是个大错误!感谢大家的帮助:)

关于java - 复制的 android db 文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com