【问题标题】:copy .db file on sdcard in android在 android 的 sdcard 上复制 .db 文件
【发布时间】:2014-11-03 06:40:54
【问题描述】:

当我的手机被植根时,此代码不会复制 sdcard 上的 .db 文件

   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();
        }

【问题讨论】:

标签: android


【解决方案1】:

将数据库从 /data/data 文件夹复制到 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);

}

【讨论】:

  • currentDB.exists() return false .如何在android中获得访问“/data/data/com.whatsapp/databases/msgstore.db”文件的权限
猜你喜欢
  • 2011-05-21
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多