0
  • how to backup application database and store in mobile storage
    folder. i try to do this way but not able to create backup file.

  try{
                File data = Environment.getDataDirectory();
                    File sd = new File(Environment.getExternalStorageDirectory()+"/digiplusBackUp");
                    if(!sd.exists()){
                        sd.mkdirs();
                    }
                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
                    String formattedDate1 = df1.format(c.getTime());
                    SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
                    String formattedDate3 = dateFormat.format(c.getTime());
                    if (sd.canWrite()) {
                    String currentDBPath = "//data//com.digiplus.live//databases//digiplus";
                    String backupDBPath = "DigiPlus"+formattedDate1+formattedDate3 ;
                    File currentDB = new File(data, currentDBPath);
                    File backupDB = new File(sd, backupDBPath);

                    if (currentDB.exists()) {
                        FileChannel src = new FileInputStream(currentDB).getChannel();
                        FileChannel dst = new FileOutputStream(backupDB).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                        Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (Exception ignored) {
            Log.d("Error",ignored.tostring());
            }
            }
  • Have you setup the manifest for permission to WRITE to EXTERNAL storage? Have you requested permission if Android is M or above. What error messages are you getting? you should edit your question to include the stack-trace (don't ignore the exception in the catch print it so you can get the stack-trace). – MikeT Jun 21 '18 at 05:37
  • i maintain all the permission on manifest. – Hardik Patel Jun 21 '18 at 05:41
  • after edit ignored case use e and its give me this error /Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins.applicationContext()' on a null object reference – Hardik Patel Jun 21 '18 at 06:28
  • try replacing `Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();`, with `Log.d("BACKUP-RESULT","backup to SD card was ok");`. odds on `getApplicationContext` is returning null; ideally add `ignored.printStackTrace();` in catch clause, if the above doesn't resoolve the issue (much better than outputting the string for debug). – MikeT Jun 21 '18 at 06:45

1 Answers1

0

this code is solve my problem thank you MikeT for your support

try {
            File data = Environment.getDataDirectory();
            File sd = new File(Environment.getExternalStorageDirectory() + "/digiplusBackUp");
            if (!sd.exists()) {
                sd.mkdirs();
            }
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
            String formattedDate1 = df1.format(c.getTime());
            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
            String formattedDate3 = dateFormat.format(c.getTime());
            if (sd.canWrite()) {
                String currentDBPath = "/data/com.digiplus.lve/databases/digiplus.db";
                String backupDBPath = "backupFolder" + formattedDate1 + formattedDate3;
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();

            }
        } catch (Exception e) {
            Log.d("Error", e.toString());
        }

    }

now i wants to restore backup so, how can i do that?

  • Basically you reverse the process i.e. copy the restore file over/to the database file. I personally take copy (rename) the database file, do the copy, if all ok delete the copy otherwise delete the db if it exists and then rename the copy back. – MikeT Jun 21 '18 at 07:54
  • same way i trying but in destination path its goes to IOException e, and showing me error like java.io.FileNotFoundException: /data/com.digipls.live/databases/digipls.db: open failed: ENOENT (No such file or directory) – Hardik Patel Jun 22 '18 at 08:34