3

我知道这已经被问了很多,但从来没有回答过。我绝对需要将文件写入根目录,没有其他解决方案。我目前使用此代码,但它在 /system/ 中没有显示任何新内容。我想将文件从我的资产复制到 /system 文件夹(带有它的子目录)

public void installFiles(View v) {
            try {
        Runtime.getRuntime().exec("su");
    } catch (IOException e) {
        mDebugView.append(e.toString());
    }
    copyPath("system/bin", "/system/bin/");
    copyPath("system/lib", "/system/lib/");
    copyPath("system/etc", "/system/etc/");
    copyPath("system/etc/audio", "/system/etc/audio/");
    copyPath("system/etc/soundimage", "/system/etc/soundimage/");
    copyPath("system/lib/soundfx", "/system/bin/soundfx/");
}

public void copyPath(String from, String to) {
    mDebugView.append("Copying path assets/" + from + " to " + to + "\n");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(from);
        for (String filename : files) {
            mDebugView.append(filename + "... \n");
            if (new File(filename).isFile()) {
                mDebugView.append("Copying " + filename + "\n");
                InputStream in = null;
                OutputStream out = null;
                in = assetManager.open(filename);
                out = new FileOutputStream(to);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
        }
    } catch (IOException e) {
        Log.e(this.getClass().toString(), e.toString());
        mDebugView.append(e.toString() + "\n");
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    mDebugView.append("..");
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}
4

4 回答 4

3

您的

Runtime.getRuntime().exec("su");

什么也没做。随着进程的创建然后被释放。要移动文件,您需要使用带有 su 的 cat 二进制文件。IE

Runtime.getRuntime().exec("su cat filepath1 > filepath2");

对于尽可能多的命令,最好获取 su 的流程实例,然后一次执行所有移动命令。

另请注意,您可能必须将系统分区挂载为 rw,因为默认情况下它可能不是 r/w。

于 2012-06-29T21:14:30.337 回答
2

您看不到任何更改,/system因为它默认安装为只读。请确保在写入文件之前重新安装它。尽管正如其他人提到su的,应该与命令一起使用。

于 2012-06-29T22:08:55.033 回答
2

我一直在挖掘,我找到了一种方法。

  1. 经过一些尝试,我的 ROM(更确切地说是 /system 文件夹)严重损坏,以至于我总是看到错误流输入:“无权限”-> 使用擦除重新安装 ROM

  2. 我将文件复制到外部存储(在本例中为 /sdcard,但请注意它可能是另一个路径 - 使用环境对象获取路径)。我将所有子文件夹提取到主文件夹。

  3. 与之前的代码一样,新代码打开了一个 SU 会话并再次关闭它。

    Process proc = runtime.exec("su");
                new ProcessReader(proc).start();
                new ErrorReader(proc).start();
                DataOutputStream os = new DataOutputStream(
                        proc.getOutputStream());
                os.writeBytes("chmod 755 /system/" + "\n");
                os.writeBytes("find /sdcard/.beats_cache/systembin -exec mv '{}' /system/bin +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemlib -exec mv '{}' /system/lib +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetc -exec mv '{}' /system/etc +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetcaudio -exec mv '{}' /system/etc/audio +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemetcsoundimage -exec mv '{}' /system/etc/soundimage +\n");
                os.writeBytes("find /sdcard/.beats_cache/systemlibsoundfx -exec mv '{}' /system/lib/soundfx +\n");
                os.writeBytes("exit\n");
                os.flush();
    
于 2012-07-01T16:53:19.687 回答
1

在开始时运行 'su' 可能不足以拥有对 /system 文件夹的写入权限。Root Explorer 和其他文件管理应用程序都必须将 /system 重新挂载为 r/w 并以只读方式重新挂载。这个问题的答案显示了重新挂载 /system 路径的命令。答案是使用 adb,但在设备上运行它应该也一样好。

附带说明一下,执行系统命令来移动文件可能比自己移动文件更容易。在我运行 Cyanogenmod 7.x 的 LG Optimus T 上,在 /system/xbin 中有cpandmv可以复制/移动文件而无需重新挂载 /system (如果是这样,可能只能通过su mvor su cp)。我对 android 的这一部分知之甚少,无法确定您(或安装您的应用程序的人)是否也会拥有这些文件,但值得研究。他们可能需要busybox,我没有调查过。

于 2012-06-29T20:54:58.680 回答