0

Possible Duplicate:
Moving my db to sd card not working

I'm trying to save a sqlite file to my sdcard. I'm using the code from this question

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

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db";
        String backupDBPath = "test_db";
        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) {
}

This is a very highly rated answer, so I would think it should work pretty simply. I get no errors in logcat. I don't see any created directory/created files. I also have the "write to external" permission in my manifest.

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 1
    start by logging your exception instead of simply catching them. `catch (Exception e) {}` of course there is nothing in the log. – njzk2 Nov 20 '12 at 08:48
  • If something died in the middle of it working wouldn't it log as an error? – EGHDK Nov 20 '12 at 08:52
  • there would be an exception thrown, it would be caught by your catch block, and nothing would be done with it, as your catch block is empty. at least put a e.printStackTrace() in it. – njzk2 Nov 20 '12 at 08:54
  • Okay, I entered the e.printStackTrace() and I ran the code and nothing came up. Also, regarding the possible duplicate, the old question and answer got the code to run with no errors in eclipse. This question is for help to debug the code to actually make it run, as it seems some logical error is keeping it from doing so. – EGHDK Nov 20 '12 at 08:57
  • You do not get logcat errors because you have an empty `catch` block! Just remove it. – CL. Nov 20 '12 at 12:04

2 Answers2

1

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

         if (sd.canWrite()) {
             String currentDBPath = "/data/packagename/databases/DATABASENAME";
             String backupDBPath = "/backup/"+"main.db";
             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();
             } else {
                 Log.e(TAG, "File does not exist: " + currentDBPath);
             }
jnr
  • 229
  • 2
  • 5
0

Don t forget to declare permission in amnifest file

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

jnr
  • 229
  • 2
  • 5
  • In the bottom of my question " I also have the "write to external" permission in my manifest." – EGHDK Nov 20 '12 at 09:28