如何在SD卡上备份和还原SqliteDatabase,SharedPreferences-Android

1 sqlite android android-backup-service

嗨,我正在开发一个应用程序,其中要将数据存储到SqliteSharedPrefrences。我从Android文档中研究过,他们说我们可以通过注册服务来做到这一点,但我认为这仅适用于Cloud Storage,因为我想将数据备份到SD卡上。我也一直在寻找FileBackHelper,但它没有给出任何清晰的信息。

请告诉您实施它的最佳方法是什么?

Mur*_*ain 5

您可以像这样将sqlite数据库复制到sdcard

private void exportDB(){
    File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String currentDBPath = "/data/"+ "com.your.package" +"/databases/"+db_name;
       String backupDBPath = SAMPLE_DB_NAME;
       File currentDB = new File(data, currentDBPath);
       File backupDB = new File(sd, 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();
        }
}
Run Code Online (Sandbox Code Playgroud)

共享首选项文件的位置通常由设备制造商选择,因此对任何共享首选项文件的硬编码路径都不好。