【问题标题】:Runtime.getRuntime().exec: why command «cp» does not work?Runtime.getRuntime().exec:为什么命令 «cp» 不起作用?
【发布时间】:2014-10-02 09:08:29
【问题描述】:

我的代码:

File dir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS );
File src = new File(dir, "test.dat");
File dst = new File(dir, "test_2.dat");

if ( src.exists() )
{
    try
    {
        Process process = Runtime.getRuntime().exec( new String[] {"cp", "-f", src.getAbsolutePath(), dst.getAbsolutePath()} );
        process.waitFor();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
}

此代码在三星 Galaxy S3 上有效(复制文件),但在亚马逊 Kindle Fire 上无效:

java.io.IOException:运行 exec() 时出错。命令:[cp, -f, /mnt/sdcard/Download/test.dat, /mnt/sdcard/Download/test_2.dat] 工作目录:null 环境:null

为什么会这样?以及如何在所有设备上执行此命令副本?

【问题讨论】:

  • 定义“作品”。它应该做什么?
  • 什么意思? (“作品”)

标签: android


【解决方案1】:

通常它不会工作,因为 android 应用程序没有访问下载目录的权限。或者可能是因为它无法访问 /bin/cp。我认为,如果您将手机植根,它将起作用。在adb shell 提示符下尝试相同的操作,然后您就会明白为什么它不起作用。

【讨论】:

  • /system/bin/sh: cp: 未找到
  • 解决了命令“dd”的问题: Runtime.getRuntime().exec( new String[] {"dd", "if="+src.getAbsolutePath(), "of="+ dst.getAbsolutePath()})
【解决方案2】:

我在替换 bootanimation.zip 时遇到了类似的问题,这就是我修复它的方法。希望它可以帮助某人。

Runtime.getRuntime().exec("mount -o remount,rw system /system");
Runtime.getRuntime().exec("cp /storage/emulated/0/bootanimation/bootanimation.zip /system/media/bootanimation.zip");
Runtime.getRuntime().exec("chmod 644 /system/media/bootanimation.zip");
Runtime.getRuntime().exec("mount -o remount,ro system /system");

【讨论】:

    最近更新 更多