gpt4 book ai didi

java - 将文件写入模拟器中的/system/app文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:03 25 4
gpt4 key购买 nike

我正在尝试将 apk 写入 Android 模拟器中的/system/app 位置。这是我的代码:

File path = Environment.getRootDirectory();
File fileToWrite = new File(path, "/" + fileName);

byte[] buff = new byte[1024];
int l = 0;
// write buffer to file
FileOutputStream fos = new FileOutputStream(fileToWrite);
while((l = zis.read(buff)) > 0){
fos.write(buff,0, l);
}
fos.close();

我的AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是,我收到以下错误消息:

java.io.FileNotFoundException: /system/MainApp-debug.apk (Read-only file system) 06-08 08:51:59.858 2921-3160/com.mainapp W/System.err: at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:287)

我设法使用相同的代码写入 /mnt/sdcard/Download 文件夹。但我不确定为什么它无法写入 /system/app 文件夹。

有什么想法吗?谢谢!

最佳答案

这是我用来创建路径的代码:

    public static File root = android.os.Environment
.getExternalStorageDirectory();

public static String path = root.getAbsolutePath() + "/" + directoryName
+ "/";

public static boolean CreateDirectory() {

boolean ret = false;
path = root.getAbsolutePath() + "/" + directoryName + "/";
File folderExisting = new File(root.getAbsolutePath(), directoryName);

if (folderExisting.exists()) {
ret = true;
} else {
ret = folderExisting.mkdir();
// ret = false;
}

return ret;
}

然后保存这些东西:

    public static boolean SaveToSD(List<String> data, String fileName) {

// File root = android.os.Environment.getExternalStorageDirectory();
boolean ret = false;
CreateDirectory();
File file = new File(path, fileName);

try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
for (int idx = 0; idx < data.size(); idx++) {
if (data.get(idx) != null)
pw.println(data.get(idx));
}

pw.flush();
pw.close();
f.close();
ret = true;
} catch (FileNotFoundException e) {
ret = false;
e.printStackTrace();
} catch (IOException e) {
ret = false;
e.printStackTrace();
}

return ret;

}

希望对你有帮助

对于对象:

        FileOutputStream fos = new FileOutputStream("file");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(mb);
oos.close();

关于java - 将文件写入模拟器中的/system/app文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756859/

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