Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

android - Write files to /system

I know this has been asked a lot but never answered. I definetly need to write files to root there is no other solution. I currently use this code but it doesn't show anything new in /system/. I want to copy file from my assets to the /system folder (with it's subdir's)

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 + "
");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(from);
        for (String filename : files) {
            mDebugView.append(filename + "... 
");
            if (new File(filename).isFile()) {
                mDebugView.append("Copying " + filename + "
");
                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() + "
");
    }
}

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your

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

does nothing. As the process is created and then released. To move files you will need to use the cat binary with su. IE

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

for as many commands as you want to do it would be better to get the process instance of su and then execute all of your move commands at once.

Also note that you may have to mount the system partition as rw as it is probably not r/w by default.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...