samedi 22 août 2015

Android Sqlite backup and restore not working

I am creating an app in which client will store records so it is very crucial to keep a back up of those records.

on backup a file is created in MyFiles in android device. when i try to restore it..i am getting FileNotFoundException.

Here is the code

  public void importData()
   {
       try {
           File sd = Environment.getExternalStorageDirectory();
           File data = Environment.getDataDirectory();

               if (sd.canWrite()) {
                   String currentDBPath="/data/"+ "com.example.mypatientsmanager" +"/databases/"+"PatientsDB";
                   String backupDBPath="PatientsRecord"; // From SD directory.
               File currentDB = new File(data, currentDBPath);
               File backupDB = new File(sd, backupDBPath);


               if(backupDB.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(ctx, "Import Successful!",
                   Toast.LENGTH_SHORT).show();
               }
               else
                   Toast.makeText(ctx, "Import UnSuccessful!",
                           Toast.LENGTH_SHORT).show();   

       }
   } catch (Exception e) {

       Log.e("error",e+"");


       Toast.makeText(ctx, "Import Failed!", Toast.LENGTH_SHORT)
               .show();

   }

`

Please help me out.

Aucun commentaire:

Enregistrer un commentaire