1

我想将一个文本文档从 apk 推送到 /system 目录(是的,它是一个适用于 root 用户的应用程序)并且想知道我将如何做到这一点:) 我的 txt 文件位于 assests 文件夹中,但如果需要可以使用它

4

2 回答 2

3

将文本文件放在项目的资产目录中,然后使用以下线程中的代码将其解压缩到文件系统: 如何将文件从“资产”文件夹复制到 sdcard?

编辑:这是我使用的一些代码。对于 sourceFileName,传入相对于资产文件夹的资产文件的名称(例如,如果资产文件夹中有 myFile.txt,则传递 myFile.txt)。对于目标文件,传递完整路径(例如 /data/data/com.mycompany/mypackage/myFile.txt)。context 是当前活动(例如 MyActivity.this)。

private boolean copyFile(Context context, String sourceFileName, String destFileName)
{
    AssetManager assetManager = context.getAssets();

    File destFile = new File(destFileName);

    File destParentDir = destFile.getParentFile();
    destParentDir.mkdir();

    InputStream in = null;
    OutputStream out = null;
    try
    {
        in = assetManager.open(sourceFileName);
        out = new FileOutputStream(destFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;

        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return false;
}

EDIT2:结果表明 /system 分区以只读方式安装,即使在有根设备上也是如此。这可能会有所帮助:Android:如何从我的 APK 中将文件系统挂载到 RW 中?(当然是有根的)

于 2012-05-16T17:52:38.063 回答
0

你可以试试这个功能(在这里找到):

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

我自己试过这样:

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

并且运作良好。这是我的输出:

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

如果你知道你的 txt 文件的绝对路径,你可以很容易地用cp.

于 2012-05-16T18:02:55.230 回答