1

I want to set on button click events backup my applications data on SD-Card. This is my code :

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

I proceed like this :

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); This line got error. How shall I solve this ? Please help.

aish
  • 25
  • 1
  • 6
  • Is it a run time or compile time error? – FD_ Feb 04 '14 at 08:30
  • 1
    Please write your questions more clear. – Seyed Hamed Shams Feb 04 '14 at 08:33
  • @HamedShams i want to set on button click event backup data from sqlite database which is used in my apps...means application is running and want to set on a restore button provide for d user for backup purpose..click on button i did use backup method above...but in dis line Files.copyFile(file, fileBackup); get a error – aish Feb 04 '14 at 08:38

2 Answers2

0

Try this:

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);
            }
        }
    } 
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • on button event call dis method?? – aish Feb 04 '14 at 08:34
  • Yes call this method on onClick yes, or wherever you want to execute. Also you can accept the answer, by clicking the tickmark next to it, it has been of any help. – Skynet Feb 04 '14 at 09:15
  • when use dis code in "fis" get error .....dis code is checking on emulator....java.io.FileNotFoundException: /data/data/com.example.judgedetail/databases/adonew.db: open failed: ENOENT (No such file or directory) – aish Feb 04 '14 at 10:30
  • Check it with a Real device, you need to create a SD Card on your emulator. – Skynet Feb 04 '14 at 10:34
0

I don't know where you copied that code from, but either you or the poster missed something. Files.copyFile() is no existing Android API function, and I guess that's what you describe as the line being not working.

You'll have to copy your file using another method, I'd suggest one like this:

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

Community
  • 1
  • 1
FD_
  • 12,947
  • 4
  • 35
  • 62