Android backup and restore database to and from SD-card

时光怂恿深爱的人放手 提交于 2019-12-30 05:03:02

问题


I'm currently looking to build a backup feature in my Android application. However I'm kinda struggling before even starting to implement it because I'm not sure what the correct way to go is.

I found some interesting articles on the net and so I came up with three possible solutions:

  1. Backup the entire DB file to the SD card
  2. Export the DB-data to an XML file on the SD card
  3. Use the Android backup mechanism to backup the entire DB to the Google cloud

Now I was wondering what you guys think about these 3 solutions, or do you know another (maybe an even better way to go) and what is in your eyes the best way to go?

Here are my remarks on the possible implementations:

  1. I don't know if the phone isn't rooted that it's possible to restore the DB file... Otherwise there aren't really any down sides for this I think...
  2. Handling XML files on the fly on Android phones is heavy so if it can be avoided it's best not to do it like that
  3. Using the Android backup mechanism the backup feature is only available if it's enabled by the user on the phone, and all the data should be copied to the cloud... Which in my case can be in some cases quite a lot...

I'm looking forward to see some input on this issue!

Thanks in advance!

Kr,

Dirk


回答1:


I always use 1.). Here's a class of mine that does backup of a DB to SD-card. I'm using FileUtils from the Apache commons-io here, you need to change that if you don't use that jar. In addition there's need for a method in your SQLiteOpenHelper class (here MySQLiteOpenHelper.getDatabaseName()) that returns the name of your database file.

You will call that from within an AsyncTask in one of your activities...

public class MyDatabaseTools {
  private String appName = "";
  private String packageName = "";

  public boolean backup() {
    boolean rc = false;

    boolean writeable = isSDCardWriteable();
    if (writeable) {
      File file = new File(Environment.getDataDirectory() + "/data/" + packageName + "/databases/" + MySQLiteOpenHelper.getDatabaseName());

      File fileBackupDir = new File(Environment.getExternalStorageDirectory(), appName + "/backup");
      if (!fileBackupDir.exists()) {
        fileBackupDir.mkdirs();
      }

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, MySQLiteOpenHelper.getDatabaseName());
        try {
          fileBackup.createNewFile();
          FileUtils.copyFile(file, fileBackup);
          rc = true;
        } catch (IOException ioException) {
          //
        } catch (Exception exception) {
          //
        }
      }
    }

    return rc;
  }

  private boolean isSDCardWriteable() {
    boolean rc = false;

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      rc = true;
    }

    return rc;
  }

    public MyDatabaseTools(final Context context, final String appName) {
        this.appName = appName;
        packageName = context.getPackageName();
    }
}



回答2:


You don't need to have a rooted phone to restore the database from a file on the SD card. Each application can write to it's own private directories, so you can just copy the file. As for 2, do you have any concrete numbers? Handling XML is fairly fast, and since it's backup/restore, it doesn't happen too often and users would expect that it takes some time, so it shouldn't be a problem. As usual, measure the actual time it takes and consider how much data you have, before you make any decisions.



来源:https://stackoverflow.com/questions/7379187/android-backup-and-restore-database-to-and-from-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!