Android的备份和恢复的数据库和SD卡 [英] Android backup and restore database to and from SD-card

查看:82
本文介绍了Android的备份和恢复的数据库和SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻求建立在我的Andr​​oid应用程序的备份功能。但是我甚至开始实现它,因为我不知道该怎么去正确的做法是之前我有点挣扎。

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. 备份整个数据库文件到SD卡
  2. 导出数据库数据到XML文件在SD卡上
  3. 使用Android的备份机制来备份整个数据库到谷歌云

现在我想知道什么你们想想这3个解决方案,或者你知道另一个(也许一个更好的方式去),什么是你的眼睛去的最好方法是什么?

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. 在我不知道,如果手机没有根,它有可能恢复数据库文件......否则,我觉得...
  2. 有没有真正的任何下跌双方为此
  3. 处理上的Andr​​oid手机飞XML文件是沉重的,所以如果能够避免,最好不要做这样的
  4. 使用Android的备份机制备份功能,如果它是由手机上的用户启用,并且所有的数据复制到云......这在我的情况下,可以在某些情况下,相当多才可用。 ..
  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!

在此先感谢!

氪,

德克

推荐答案

我总是用1)。下面是一类矿井,做一个DB到SD卡的备份。我使用从Apache公地IO这里文件实用,你需要改变,如果你不使用的罐子。此外,还有的需要的一种方法,你SQLiteOpenHelper类(这里MySQLiteOpenHelper.getDatabaseName()),返回你的数据库文件的名称。

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.

您将调用来自内部的AsyncTask的,在你的活动之一......

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

这篇关于Android的备份和恢复的数据库和SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆