【发布时间】:2013-12-04 07:47:55
【问题描述】:
我正在做一个应用程序来备份和恢复 Android 设备中所有用户安装的应用程序。我能够备份用户选择备份的所有应用程序的 apk 文件。现在我必须将应用程序的数据文件与apk文件一起备份。
我正在尝试这样的事情
public void copyDbToSdcard(String packageName) {
InputStream myInput;
String dbpath = "/data/"+packageName+"/databases/";
String sdpath = Environment.getExternalStorageDirectory().getPath();
try {
File exist=new File(Environment.getDataDirectory()+dbpath);
System.out.println("1 "+ exist.exists());
// Set the output folder on the Scard
File directory = new File(sdpath + "/Back");
// Create the folder if it doesn't exist:
if (!directory.exists()) {
directory.mkdirs();
}
// Set the output file stream up:
myInput = new FileInputStream(Environment.getDataDirectory()
+ dbpath);
OutputStream myOutput = new FileOutputStream(directory.getPath()
+ "/refuel_db");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
Toast.makeText(MainActivity.this, "Backup Done Succesfully!", Toast.LENGTH_LONG)
.show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我收到错误日志
EACCES (Permission denied)上线
myInput = new FileInputStream(Environment.getDataDirectory()+ dbpath);
我已经在清单文件中授予了 READ 和 WRITE 权限
【问题讨论】:
-
你不能读取其他应用的隐私数据,除非他们设置了世界读取权限。您还错误地构建了路径(dbpath 是绝对的,因此请单独使用它),但这并不重要,因为即使正确它也不起作用。