gpt4 book ai didi

android - 如何在 Android 中设置文件的可执行权限以使用 "Runtime.getRuntime().exec(..)"执行

转载 作者:行者123 更新时间:2023-11-30 02:59:17 25 4
gpt4 key购买 nike

我目前正在尝试从我的应用程序主目录中的 Android 项目本地调用 shell 上的可执行文件。但是,在使用 ADB 时,我可以通过命令行看到我的文件没有获得可执行文件权限。我尝试了以下方法:

File file = new File(myFileLocation);
if(file.exists()){
boolean executable = file.setExecutable(true);
}

“可执行文件”仍然为假。

我也尝试过以下方法:

Process processChmod = Runtime.getRuntime().exec("/system/bin/chmod u+x " + myExecutableLocation);
processChmod.waitFor();

当我尝试对进程发出命令时,出现以下 IOException:

java.io.IOException: Permission denied java.io.IOException: Error running exec(). Command: [/storage/emulated/0/myApp/iperf, -c, iperf.eltel.net] Working Directory: null Environment: null

我要发出的命令是:

Runtime.getRuntime().exec("/storage/emulated/0/myApp/iperf -c iperf.eltel.net")

同样,在这个阶段,我可以看到我希望使用的进程的文件权限只是 r/w 而不是可执行文件。任何帮助表示赞赏!顺便说一下,我正在尝试使用“iPerf”C 库,并且有一个已编译的 armeabi 模块以及原始源代码/JNI 代码。没有编译问题,这看起来更像是文件权限问题。

最佳答案

位于 SD 卡上的文件不可执行。将文件移动到内部存储(例如/data/local,或者如果您的权限被拒绝,则移动到 data/local/tmp)。

我就是这样做的:

String filePath = getFilesDir().getAbsolutePath() + File.separator + "myFileName";
File myFile = new File(filePath);
if (!myFile.canExecute()) {
Log.d("File is not executable, trying to make it executable ...");
if (myFile .setExecutable(true)) {
Log.d("File is executable");
} else {
Log.d("Failed to make the File executable");
}
} else {
Log.d("File is executable");
}

关于android - 如何在 Android 中设置文件的可执行权限以使用 "Runtime.getRuntime().exec(..)"执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com