【发布时间】:2014-10-17 15:40:28
【问题描述】:
我想知道是否可以执行“cp”linux 命令将文件从我的应用程序的 assets 文件夹复制到 /system/bin 分区(显然具有 root 访问权限)。
Utility.exec("cp <file:///android_asset/my_file> /system/bin");
此代码也可以将文件从资产复制到系统/bin?
【问题讨论】:
我想知道是否可以执行“cp”linux 命令将文件从我的应用程序的 assets 文件夹复制到 /system/bin 分区(显然具有 root 访问权限)。
Utility.exec("cp <file:///android_asset/my_file> /system/bin");
此代码也可以将文件从资产复制到系统/bin?
【问题讨论】:
此代码也可以有效地将文件从资产复制到系统/bin?
没有。
首先,Linux cp 命令不使用方案,AFAIK。至少,它不适用于 Ubuntu。
其次,file:///android_asset/ URL 前缀几乎只用于WebView。
第三,资产不是 Android 设备上的文件。它们是作为 APK 文件的 ZIP 存档中的条目。 cp 命令适用于文件。
欢迎您使用AssetManager 和 Java 文件 I/O 代码将资产复制到本地文件。
【讨论】:
这是一个干净的例子,假设获得了 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) {}
}
【讨论】:
dd 使用单个参数调用,例如 " of=/system/bin/this_is_a_bad_idea”。当然 /system 通常也以只读方式挂载。