【问题标题】:Cp command android from assets folder资产文件夹中的cp命令android
【发布时间】:2014-10-17 15:40:28
【问题描述】:

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

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

此代码也可以将文件从资产复制到系统/bin?

【问题讨论】:

    标签: java android linux


    【解决方案1】:

    此代码也可以有效地将文件从资产复制到系统/bin?

    没有。

    首先,Linux cp 命令不使用方案,AFAIK。至少,它不适用于 Ubuntu。

    其次,file:///android_asset/ URL 前缀几乎只用于WebView

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

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

    【讨论】:

      【解决方案2】:

      这是一个干净的例子,假设获得了 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) {}
      }
      

      【讨论】:

      • 这行不通,因为"root" 不适用于android app的进程,而且很难让自定义java代码运行在stand类型可以应用的单独进程(如果设备上存在这样的 hack)。
      • 很高兴知道。所以不可行?
      • 如果有 root hack,应该可以在 android 应用进程中从资产中获取数据并将其通过管道传输到本地可执行文件中,例如 dd 使用单个参数调用,例如 " of=/system/bin/this_is_a_bad_idea”。当然 /system 通常也以只读方式挂载。
      猜你喜欢
      • 2013-10-14
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 2011-01-22
      • 2010-12-28
      • 2011-10-01
      • 1970-01-01
      相关资源
      最近更新 更多