将数据库备份到SD卡

时间:2013-04-02 19:43:59

标签: java android android-backup-service

我尝试在我的应用中制作备份功能,但它会产生强制关闭错误

任何人都可以在我的代码中给出一些错误的想法

请帮帮我

这是我的代码

public class Mydatabase {

 private String appName = "";
  private String packageName = "";
  public static final String DATABASE_NAME = "data.db";
  public boolean backup() {
    boolean rc = false;

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

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

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, DATABASE_NAME);
        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 Mydatabase(final Context context, final String appName) {
        this.appName = appName;
        packageName = context.getPackageName();
    }

}

这是我的日志

04-03 00:39:18.595: E/AndroidRuntime(674): FATAL EXCEPTION: main
04-03 00:39:18.595: E/AndroidRuntime(674): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.neelrazin.noteit/com.neelrazin.noteit.Mydatabase}: java.lang.InstantiationException: com.neelrazin.noteit.Mydatabase
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.os.Looper.loop(Looper.java:123)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 00:39:18.595: E/AndroidRuntime(674):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 00:39:18.595: E/AndroidRuntime(674):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 00:39:18.595: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 00:39:18.595: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 00:39:18.595: E/AndroidRuntime(674):  at dalvik.system.NativeStart.main(Native Method)
04-03 00:39:18.595: E/AndroidRuntime(674): Caused by: java.lang.InstantiationException: com.neelrazin.noteit.Mydatabase
04-03 00:39:18.595: E/AndroidRuntime(674):  at java.lang.Class.newInstanceImpl(Native Method)
04-03 00:39:18.595: E/AndroidRuntime(674):  at java.lang.Class.newInstance(Class.java:1409)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-03 00:39:18.595: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-03 00:39:18.595: E/AndroidRuntime(674):  ... 11 more

2 个答案:

答案 0 :(得分:3)

你似乎有点失落。 这是备份数据库的功能方法,您可以将它放在任何类中。

public static void backupDatabase() throws IOException {
    //Open your local db as the input stream
    String inFileName = "/data/data/com.android.exemple/databases/databename.db";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()+"/database.db";
    //Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0){
        output.write(buffer, 0, length);
    }
    //Close the streams
    output.flush();
    output.close();
    fis.close();
}

您需要在清单中的应用元素

之前添加此行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

不要忘记更改inFileName变量中的包名称和两个地方的数据库名称。

答案 1 :(得分:0)

该跟踪指出您尝试发起名为Activity的{​​{1}}这一事实。正如我们从您的代码段中看到的那样,Mydatabase不会扩展Mydatabase。系统正在寻找要启动的Activity,而不是仅从Activity派生的类。