gpt4 book ai didi

android - 如何以编程方式将应用程序数据复制到 android 中的 sdcard?

转载 作者:太空狗 更新时间:2023-10-29 15:07:36 25 4
gpt4 key购买 nike

我正在做一个应用程序来备份和恢复 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(权限被拒绝) 在线

myInput = new FileInputStream(Environment.getDataDirectory()+ dbpath);

我已经在 list 文件中授予了读和写权限

最佳答案

尝试在

中使用 /sdcard
String dbpath = "/sdcard/"+packageName+"/databases/";

而不是/data 因为它属于“内部存储”。

关于android - 如何以编程方式将应用程序数据复制到 android 中的 sdcard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com