0

I am a newbie in android programming and looking for your help. I am working on creating an android application that saves data into Sqlite database.. I managed that and could view the data from the emulator after I pulled it out. Now I am seeking a way to copy this internal sqlite to SD card pro grammatically.

I searched the solution and I found this :

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) {
    }

However, I do not know how to integrate this piece of code to my entire code. Should I write it at each time I insert data ? I would be so grateful for any help I may get.. Any further explanations or other suggestions would be appreciated.

Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
Hayder
  • 31
  • 7
  • If your goal is to have a sqlite database that can be accessed by every app on your android device you can use and create the databse in public internal memory instead of app-private memory as described in https://stackoverflow.com/questions/5332328/sqliteopenhelper-problem-with-fully-qualified-db-path-name/9168969#9168969. with this usecase there is no more need to copy db from app-internal-memory to public-internal-memory – k3b Nov 20 '17 at 13:35

1 Answers1

0

Firstly you can break, if you do after every click. I think you should store at periodic times using AlarmManager.

dralexnumber
  • 238
  • 2
  • 10
  • I am a bit fortune and I solve this by using ADB shell. It is also good to mention that my phone is rooted. – Hayder Nov 21 '17 at 07:29