如何在sqlite数据库中设置按钮备份/恢复功能?

时间:2014-02-04 08:27:08

标签: android sqlite

我想设置按钮点击事件备份SD卡上的应用程序数据。 这是我的代码:

              shv.setOnClickListener(new OnClickListener() {  
                    @Override
                    public void onClick(View arg0) {
                        Toast.makeText(SecondActivity.this, "backup Button press",Toast.LENGTH_LONG).show();
                        boolean rc = MyDatabaseTools.backup();
                        System.out.println(rc);
                        if(rc==true)
                        {
                            Toast.makeText(SecondActivity.this, "backup Successfully",Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(SecondActivity.this, "backup error",Toast.LENGTH_LONG).show();
                        }   
                    }
                });

我这样做:

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

  public static boolean backup() {
    boolean rc = false;

    boolean writeable = isSDCardWriteable();
    if (writeable) {
      File file = new File(Environment.getDataDirectory() + "/data/<com.example.judgedetail>/databases/ado.db");

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

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, "ado.db");
        try {
          fileBackup.createNewFile();
          /*Files.copyFile(file, fileBackup);*/  //got error here dis line not work
          rc = true;
        } catch (IOException ioException) {
          //
        } catch (Exception exception) {
          //
        }
      }
    }

    return rc;
  }

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

}

Files.copyFile(file,fileBackup); 这条线出错了。 我该如何解决这个问题? 请帮忙。

2 个答案:

答案 0 :(得分:0)

试试这个:

public void writeToSD() throws IOException {
        File f=new File("/data/data/com.YourPackageName/databases/DBName");
        FileInputStream fis=null;
        FileOutputStream fos=null;

        try{
            fis=new FileInputStream(f);


            fos=new FileOutputStream("/mnt/sdcard/dump.db");
            while(true){
                int i=fis.read();
                if(i!=-1){
                    fos.write(i);
                }
                else{
                    break;
                }
            }
            fos.flush();

        }
        catch(Exception e){
            e.printStackTrace();

        }
        finally{
            try{
                fos.close();
                fis.close();
            }
            catch(IOException ioe){
                System.out.println(ioe);
            }
        }
    } 

答案 1 :(得分:0)

我不知道你从哪里复制了这些代码,但是你或者海报错过了什么。 Files.copyFile()不存在Android API函数,我想这就是您所描述的行不起作用。

你必须使用其他方法复制你的文件,我建议像这样:

https://stackoverflow.com/a/9293885/1691231