复制的 android db 文件为空

发布于 2024-12-01 00:22:32 字数 1048 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-12-08 00:22:32

@piyush
感谢您的尝试/捕获通知。在 catch 块的 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() 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.

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;
}

}

My DB_PATH was already set to /data/data/package/databases/data.db and added to Environment.getDataDirectory() the dbFile result to /data/data/data/package/databases/data.db
That's the big mistake! Thanks all for help :)

走过海棠暮 2024-12-08 00:22:32

在使用 transferTo(...); 之前尝试使用 outChannel.force(true);

try using outChannel.force(true); before using the transferTo(...);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文