【问题标题】:Is it possible to save database file to SD card? [duplicate]是否可以将数据库文件保存到 SD 卡? [复制]
【发布时间】:2011-12-06 09:23:38
【问题描述】:

可能重复:
Is it possible to copy database file to SD card?

我的 Android 手机上有一个数据库,我需要将信息存入 SD 卡。

是否可以将数据库文件以可读状态保存到 SD 卡上?我无法找到有关如何执行此操作的任何信息。我知道数据库的名称和字段等...

我找到了一些展示如何保存到 SD 卡的示例,但并不完全符合我的需要。

将数据库文件复制到 SD 卡的一些源代码将是完美的。

希望这个问题足够清楚。

【问题讨论】:

  • LAS_VEGAS 以何种方式没有回答您的问题?请不要重复提问 - 让您的问题可见的礼貌方式是 post a bounty
  • 第一个问题是无意发帖的。这不是礼貌问题,而是在我注意到我的浏览器在我写完问题之前发布之前,承认回答我问题的人的贡献。特别是因为这两个问题都包含值得认可的好答案。如果我是主持人并且可以合并答案,我会的,但我不能,所以我没有。在我看来,删除另一个问题会比提供两种不同的解决方案更糟糕。 (一个是概念性的,第二个是功能代码示例)。
  • Dyarish,好吧,这更有意义,谢谢。但不要忽视edit 按钮,它允许您(和其他人)改进问题或答案。 :)
  • 当然。这里的人太好了,在意外发布后不到两分钟就有了一个很好的答案。不过,非常有效的观点,谢谢萨诺德。 :)

标签: java android database save sd-card


【解决方案1】:

是的。这是我使用的功能:

public void copyDBToSDCard() {
    try {
        InputStream myInput = new FileInputStream("/data/data/com.myproject/databases/"+DATABASE_NAME);

        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                Log.i("FO","File creation failed for " + file);
            }
        }

        OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        Log.i("FO","copied");

    } catch (Exception e) {
        Log.i("FO","exception="+e);
    }


}

对于我从事的项目,我在主屏幕中放置了一个菜单选项,我可以随时从中调用此功能。然后,我会将数据库移动到我的桌面并使用 FireFox 的 SQLite Manager 插件打开它。

【讨论】:

  • 非常感谢 SBerg413!这非常有效。
  • 感谢它运行良好。
【解决方案2】:

当然。如果这是您的应用程序中存在的数据库,您可以通过Context.getDatabasePath() 获取对 db 文件的引用,并将数据库名称传递给它。从那里开始,它只是一个例行的文件复制操作:

//Get a reference to the database
File dbFile = mContext.getDatabasePath("mydb");
//Get a reference to the directory location for the backup
File exportDir = new File(Environment.getExternalStorageDirectory(), "myAppBackups");
if (!exportDir.exists()) {
  exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation String command = params[0];

//Attempt file copy
try {
  backup.createNewFile();
  fileCopy(dbFile, backup);
} catch (IOException e) {
  /*Handle File Error*/
}

其中方法fileCopy()定义为:

private void fileCopy(File source, File dest) throws IOException {
  FileChannel inChannel = new FileInputStream(source).getChannel();
  FileChannel outChannel = new FileOutputStream(dest).getChannel();
  try {
    inChannel.transferTo(0, inChannel.size(), outChannel);
  } finally {
    if (inChannel != null) inChannel.close();
    if (outChannel != null) outChannel.close();
  }
}

HTH!

【讨论】:

  • 感谢 Devunwired!我希望我可以将两个答案标记为正确,因为这也是一个很好的解决方案。绝对值得一票。干杯!
  • 我希望这可以被标记为答案;它非常干净和精确。
猜你喜欢
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多