从资源文件夹命令cp命令android

时间:2014-10-17 15:40:28

标签: java android linux

我想知道是否可以执行“cp”linux命令将文件从我的应用程序的assets文件夹复制到/ system / bin分区(显然具有root访问权限)。

        Utility.exec("cp <file:///android_asset/my_file> /system/bin");

此代码可以将文件从资产复制到system / bin吗?

2 个答案:

答案 0 :(得分:1)

  

此代码可以将文件从资产复制到system / bin吗?

没有

首先,Linux cp 命令不使用方案AFAIK。最少,它在Ubuntu上不起作用。

其次,file:///android_asset/网址前缀几乎只适用于WebView

第三,资产不是Android设备上的文件。它们是ZIP存档中的条目,即APK文件。 cp 命令适用于文件。

欢迎您使用AssetManager和Java文件I / O代码将资产复制到本地文件。

答案 1 :(得分:0)

这是一个干净的例子,假设获得了root:

AssetManager assetManager = context.getAssets();

InputStream is = null;
OutputStream os = null;
try
{
    is = assetManager.open("my_file");
    os = new FileOutputStream("/system/bin/my_file");

    byte[] buffer = new byte[1024];
    int length;

    // copy the file content in bytes 
    while ((length = is.read(buffer)) > 0)
    {
        os.write(buffer, 0, length);
    }
}
catch (Exception e)
{
    // Dealing with the exception, log or something
}
finally
{
    if (is != null) try { is.close(); } catch (IOException e) {}
    if (os != null) try { os.close(); } catch (IOException e) {}
}
相关问题