简体   繁体   English

Runtime.getRuntime()。exec:为什么命令“ cp”不起作用?

[英]Runtime.getRuntime().exec: why command «cp» does not work?

My code: 我的代码:

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();
    }
}

This code works (copy file) on Samsung Galaxy S3, but does not work on Amazon Kindle Fire: 该代码(复制文件)在Samsung Galaxy S3上有效,但在Amazon Kindle Fire上无效:

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

Why does this happen? 为什么会这样? And how to get to execute this command copy on all devices? 以及如何在所有设备上执行此命令副本?

In general it won't work because an android application doesn't have premissions to access the downloads directory. 总的来说,这是行不通的,因为android应用程序没有权限可以访问downloads目录。 Or maybe because it can't access /bin/cp. 也许是因为它无法访问/ bin / cp。 It will work if you root your phone I think. 我想,如果您扎根手机,它将正常工作。 Try the same from the adb shell prompt so you then understand why it doesn't work. adb shell提示符下尝试相同的操作,这样您就可以理解为什么它不起作用。

I have had a similar issue with replacing the bootanimation.zip and this is how I fixed it. 我在替换bootanimation.zip时遇到了类似的问题,这就是我修复它的方式。 Hope it helps somebody. 希望它能帮助到别人。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM