Tuesday 22 April 2014

( Copy DB file to /mnt/sdcard ) And ( Copy DB file From /mnt/sdcard )

         
// updated on 12-12-2016

public static void copyAppDbToDownloadFolder(Context ctx) {
   try {
      File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "BK_NITHIN.db"); // for example "my_data_backup.db"
      File currentDB = ctx.getDatabasePath("NITHIN.db");
      if (currentDB.exists()) {
         FileInputStream fis = new FileInputStream(currentDB);
         FileOutputStream fos = new FileOutputStream(backupDB);
         fos.getChannel().transferFrom(fis.getChannel(), 0, fis.getChannel().size());
         fis.close();
         fos.close();
         System.out.println("Copying Database success, reason:");

      } else  System.out.println("Copying Database fail, reason:");
   } catch (IOException e) {
      System.out.println("Copying Database fail, reason:");
   }
}






 private static Context ctx;
private static String DB_PATH = "/data/data/<package name>./databases/";
private static String DB_NAME = "DbName-Here";




                                 Copy DB file to /mnt/sdcard


public static void pullDB(Context ctx) {

File curDB = ctx.getDatabasePath("//DbName-Here");
File bakupDB = new File(Environment.getExternalStorageDirectory(),
"DbName-Here");

try {
FileChannel src = new FileInputStream(curDB).getChannel();
FileChannel dst = new FileOutputStream(bakupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
Toast.makeText(
ctx,
"DB copied to " + Environment.getExternalStorageDirectory(),
Toast.LENGTH_LONG).show();
}
}


Copy DB file From  /mnt/sdcard



public static void pushDB(Context ctx) {

try {

InputStream mInputStream = ctx.getAssets()
.open("DbName-Here");
String outFileName = ctx.getDatabasePath("DbName-Here")
.toString();
OutputStream mOutputStream = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = mInputStream.read(buffer)) > 0) {
mOutputStream.write(buffer, 0, length);
}
mOutputStream.flush();
mOutputStream.close();
mInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

No comments:

Post a Comment

How to do text writing animation?  In Android Development we do not have the option to use any other custom font in our default Tex...