【问题标题】:Writing files to /system/app folder in emulator将文件写入模拟器中的 /system/app 文件夹
【发布时间】:2018-06-08 08:53:47
【问题描述】:

我正在尝试将一个 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 (只读 文件系统) 06-08 08:51:59.858 2921-3160/com.mainapp W/System.err:在 java.io.FileOutputStream.open0(本机方法) 在 java.io.FileOutputStream.open(FileOutputStream.java:287)

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

有什么想法吗?谢谢!

【问题讨论】:

  • 你的代码是正确的问题是模拟器文件系统。您要写入的位置是只读的
  • 有什么办法可以改吗?
  • Nope 你尝试在移动设备上运行该应用程序,如果它运行良好,它的明显模拟器有问题
  • 与模拟器无关。该路径是只读的。开始根您的设备。
  • @greenapps 我明白了,所以问题是因为模拟器没有植根?这就是我无法对其执行“su”命令的原因吗?

标签: java android emulation


【解决方案1】:

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

    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();

【讨论】:

  • 我明白了,但它是否需要有根设备?您所做的是在 system/app 文件夹下创建一个目录,然后将文件存储到其中,对吗?
  • 我不知道有没有,但一般来说你是对的,请测试一下
  • 嘿,我可以知道 data 参数是做什么用的吗?因为到目前为止我只有一个 File 对象
  • 所以然后使用你想要的任何东西 insted List 它会正常工作你应该删除这段代码的一部分使用 ObjectOutputStream
猜你喜欢
  • 2018-06-05
  • 2016-06-28
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
相关资源
最近更新 更多