如何使用Java将数据库从/data/data/my.package.name/databases/
复制到/mnt/shared
文件夹?
我尝试使用FileOutputStream
,但获得open failed: EACCES (Permission denied)
答案 0 :(得分:0)
使用此:
public void writeToSDCard() throws IOException {
System.out.println("Write To Sd");
File f=new File("/data/data/com.yourpackage/databases/dbname");
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/dumped.db");
while(true){
int i=fis.read();
if(i!=-1){
fos.write(i);
}
else{
break;
}
}
fos.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
fos.close();
fis.close();
}
catch(IOException ioe){
// System.out.println(ioe);
}
}
System.out.println("Wrote to Sd");
}
在Manifest中添加所需的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
我能够找到问题的解决方案。需要手机的根。
public static void copyDb() {
try {
String command = "cp -r /data/data/my.package.name/databases/ /mnt/shared/";
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("chmod 777 " + sharedFolder + " \n");
os.flush();
os.writeBytes(command + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
suProcess.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}