简体   繁体   中英

copy .db file on sdcard in android

this code does not copy .db file on sdcard while my phone is rooted

   try {
            String comando = "cp -r /data/data/com.whatsapp/databases/msgstore.db /storage/sdcard0/tmp";
            Process suProcess = Runtime.getRuntime().exec("su");
            System.out.println(">>>>"
                    + Environment.getExternalStorageDirectory());
            DataOutputStream os = new DataOutputStream(
                    suProcess.getOutputStream());
            os.writeBytes(comando + "\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            try {
                int suProcessRetval = suProcess.waitFor();
                if (255 != suProcessRetval) {
                    //
                    System.out.println(">>>>> done >>>>");
                } else {
                    //
                    System.out.println(">>>>> not done >>>>");
                }
            } catch (Exception ex) {
                Log.e("ERROR-->", ex.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Copy database from /data/data folder to sdcard :

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

            if (sd.canWrite()) {
                String currentDBPath = "//data//com.example.usarmy//databases//usarmy_db.sqlite";
                String backupDBPath = "backdatabase.sqlite";
                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) {
         System.out.println("error in data base copy:"+e);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM