【问题标题】:Copying a file from withing a apk to the internal storage将文件从 apk 中复制到内部存储
【发布时间】:2012-05-24 07:44:06
【问题描述】:

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

【问题讨论】:

  • 不是真的,我现在想知道如何将 txt 文档从我的 apk 中复制到 /system 目录。怎么模糊? (老实说,这并不意味着听起来粗鲁)
  • 很多东西我需要问哪些是简单的,但是所有的安卓聊天室都因为一些愚蠢的原因被锁定了!网上也没有信息(我一直在谷歌上搜索!)
  • 首先,push 是一个糟糕的术语选择。您也没有具体说明您到目前为止所做或尝试过的事情。
  • 啊,好吧,我不知道从哪里开始。我真的想从我的 apk 将文件复制到 /system 。我该从哪里开始?

标签: android


【解决方案1】:

将文本文件放在项目的资产目录中,然后使用以下线程中的代码将其解压缩到文件系统: How to copy files from 'assets' folder to 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: how to mount filesystem in RW from within my APK? (rooted, of course)

【讨论】:

  • 在目标文件中只是实际文件,例如。 /media/txt.txt 还是应该是 /media/(这些是由目录和文件名组成的 XD)
  • 这是文件系统上的实际文件路径。我记得的另一件事是 /system 在正常操作期间以只读方式安装,即使在有根设备上也是如此。所以你需要重新安装它来读写:stackoverflow.com/questions/5481395/…
  • 是的,我知道我必须这样做,由于某种原因,我正在努力将此代码放在 onclick 侦听器中,有什么帮助吗?
  • 我更改了函数代码以在函数定义中包含上下文。只需从 onClick 侦听器调用该函数为copyFile(MyActivity.this, "myFile.txt", "/system/myFile.txt")。如果您负责读写分区安装,应该可以工作。
  • 现在就试试吧 :) 非常感谢您的支持!
【解决方案2】:

你可以试试这个功能(找到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);
    }       
}

我自己也试过这样:

    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复制它。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2015-09-14
    • 1970-01-01
    • 2018-01-05
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多