0

Is it possible to copy the .db file such as mmssms.db or user_dict.db to the SDCard?

If it is possible then all I need to do is, read the db file and write it to the SDCard.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Dinash
  • 3,027
  • 4
  • 32
  • 45

2 Answers2

1

You could start with this sample:

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

   if (sd.canWrite()) {
      String currentDBPath = "\\data\\{package name}\\databases\\{database name}";
      String backupDBPath = "{database name}";
      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();
      }
   }
} catch (Exception e) {
   // exception
}
evilone
  • 22,410
  • 7
  • 80
  • 107
  • thanks for the response... i get ` java.io.FileNotFoundException: /data//data/com.android.providers.telephony/databases/mmssms.db (Permission denied)` error while running it. what permission i need to set for that... – Dinash Jun 22 '11 at 08:47
  • @Dinash WRITE_EXTERNAL_STORAGE is needed for that for example...But mmssms.db is not available to developers on any production Android device. – evilone Jun 22 '11 at 08:50
  • ya i have added but i think the permission denied comes when reading from that path... – Dinash Jun 22 '11 at 08:53
  • Thanks buddy.... I was able to take the back up once i set the 775 permission to the .db file... Is it possible to execute the adb chmod command from an activity... – Dinash Jun 22 '11 at 10:11
  • that code work if you implement it in the same application but fit you try do a second application for backup the database of first application this does not work and get that error "permission denied" if you have "WRITE_EXTERNAL_STORAGE" this does not solutionate the problem, yet i have not found a solution for this. – angel Nov 07 '13 at 16:11
1
// Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();
Android
  • 8,995
  • 9
  • 67
  • 108