Copying a file from withing a apk to the internal storage

后端 未结 2 1723
感情败类
感情败类 2020-12-20 07:21

I would like to push a text document from the apk into the /system directory (Yes its a app for rooted users) and was wondering how i would do this :) My txt file is in the

2条回答
  •  独厮守ぢ
    2020-12-20 07:53

    You can try this function (found here):

    public String runSystemCommand(String cmd)
    {
        try {
            // Executes the command.
            Process process = Runtime.getRuntime().exec(cmd);
    
            // Reads stdout.
            // NOTE: You can write to stdin of the command using
            //       process.getOutputStream().
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();
    
            // Waits for the command to finish.
            process.waitFor();
    
            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }       
    }
    

    I've tryed it myself in this way:

        String cmdoutput = this.runSystemCommand("/system/bin/ls .");
        Log.d("SampleAndroidInterfaceActivity", "runSystemCommand() returned: " + cmdoutput);
    

    and works well. This is my output:

    05-16 17:50:10.423: runSystemCommand() returned: acct
    05-16 17:50:10.423: cache
    05-16 17:50:10.423: config
    05-16 17:50:10.423: d
    05-16 17:50:10.423: data
    05-16 17:50:10.423: default.prop
    05-16 17:50:10.423: dev
    05-16 17:50:10.423: etc
    05-16 17:50:10.423: init
    05-16 17:50:10.423: init.goldfish.rc
    05-16 17:50:10.423: init.rc
    05-16 17:50:10.423: mnt
    05-16 17:50:10.423: proc
    05-16 17:50:10.423: root
    05-16 17:50:10.423: sbin
    05-16 17:50:10.423: sdcard
    05-16 17:50:10.423: sys
    05-16 17:50:10.423: system
    05-16 17:50:10.423: ueventd.goldfish.rc
    05-16 17:50:10.423: ueventd.rc
    05-16 17:50:10.423: vendor
    

    If you know the absolute path of your txt file, you can easily copy it with a cp.

提交回复
热议问题