0

我正在尝试将文件从/path/to the/file.ext(是的,它有空格,我怀疑这至少是问题的一部分)复制到/data/data/com.my_pkg.app/file.ext.

在我的(根)应用程序中,我这样做:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";
try {
    Process process;
    process = new ProcessBuilder(cmd).start();
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

但这引发了IOException: no such file or directory.

检查我的理智,我打开了一个根文件资源管理器应用程序,导航到上面的路径,复制它,并将其粘贴到我的应用程序目录中,一切都很好。

为什么cp找不到呢?- 文件资源管理器应用程序必须在所有 GUI 下做同样的事情(好吧,通过修改使其工作!)。

编辑:完整错误:

W/System.err( 2441): java.io.IOException: Error running exec(). 
     Command: [su -c "cp /path/to\ the/file.ext /data/data/com.my_pkg.app/file.ext"] 
     Working Directory: null 
     Environment: [ANDROID_ROOT=/system, 
          EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, 
          LOOP_MOUNTPOINT=/mnt/obb, 
          EMULATED_STORAGE_TARGET=/storage/emulated, 
          ANDROID_BOOTLOGO=1, LD_LIBRARY_PATH=/vendor/lib:/system/lib,             
          EXTERNAL_STORAGE=/storage/emulated/legacy, 
          ANDROID_SOCKET_zygote=10, 
          ANDROID_DATA=/data, 
          PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, 
          ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, 
          BOOTCLASSPATH=/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/webviewchromium.jar, 
          ANDROID_PROPERTY_WORKSPACE=9,0, 
          ANDROID_STORAGE=/storage]
4

1 回答 1

1

I'm posting this as an answer, because it resolved my question as I asked it, and so should solve this problem for any one happening across this by search. For me, however, it just led to another question.

By changing the string:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";

to an array (that the rest of the code as-is concatenates/builds):

String[] cmd = {"su", "-c", "\"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\""};

All the errors were resolved. I can't tell you why, but at least they are.

于 2014-02-27T20:21:59.597 回答