java - 将文件写入模拟器中的 /system/app 文件夹
问题描述
我正在尝试在 Android Emulator 中将 apk 写入 /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 (只读文件系统) 06-08 08:51:59.858 2921-3160/com.mainapp W/System.err: at java.io.FileOutputStream.open0 (本机方法)在 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();
推荐阅读
- flutter - Flutter Inherited Widget - 缺少一些监听器
- listview - 如何在 Nativescript-vue 中创建具有交错或网格布局的 ListView?
- unit-testing - 我们是否应该对谓词方法进行单元测试
- python - 气流任务无法连接到远程 Oracle 数据库
- javascript - 如何在odoo中覆盖js函数
- c# - 为什么我们使用 ObservableCollection
在 UWP/C# 中? - docker - Gcloud Kubernetes 连接拒绝暴露服务
- python - 根据另一个数据框中的数据为 pandas 列赋值
- javascript - 两个同名函数,我想调用 div 处于活动状态的函数
- c++ - 如何编写与此汇编代码相等的 c/c++ 代码?