【问题标题】:Copy database from sdcard to data/data/packagename/databases/ folder将数据库从 sdcard 复制到 data/data/packagename/databases/ 文件夹
【发布时间】:2014-03-20 11:38:10
【问题描述】:

在我的应用程序中,我通过下载管理器类从服务器下载数据库,并将其默认存储在“sdcard”中,所以我想将“sdcard”中的下载数据复制到数据/数据文件夹。所以请任何人可以帮助我?

【问题讨论】:

  • 你先用谷歌搜索了吗?

标签: android android-sdcard


【解决方案1】:

试试下面的函数将你的数据库从 sdcard 复制到你的应用程序包中。

private void exportDB(){
    File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String currentDBPath = "<your sdcard path>";
       String backupDBPath = "data/data/"+ "<yourpackagename>" +"/databases/"+SAMPLE_DB_NAME;;
       File currentDB = new File(sd, currentDBPath);
       File backupDB = new File(data, backupDBPath);
       try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
        } catch(IOException e) {
            e.printStackTrace();
        }
}

根据您的数据库下载路径和要备份的路径更改currentDBPath 和backupDBPath。

【讨论】:

  • backupDB 应该替换为 context.getDatabasePath(SAMPLE_DB_NAME)
猜你喜欢
  • 1970-01-01
  • 2013-02-24
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 2015-01-18
相关资源
最近更新 更多