我将bash脚本放在Android Studio Project的/res/raw/
文件夹中。如何使用exec?
我试着这样做:
InputStream scriptFile = activity.getResources().openRawResource(R.raw.file); // script path
但我无法获取文件路径
答案 0 :(得分:3)
你可以像这样执行你的bash:
BufferedReader r = new BufferedReader(new InputStreamReader(scriptFile));
String command = "";
List<String> commands = new ArrayList<>();
try {
command = r.readLine();
commands.add(command);
while(command != null){
command = r.readLine();
commands.add(command);
}
Process p = new ProcessBuilder().command(commands).start();
} catch (IOException e) {
e.printStackTrace();
}
您可以获取原始资源的Uri以创建新的File对象:
Uri uri = Uri.parse("android.resource://com.your.package/raw/filename");
答案 1 :(得分:-4)
或者你可以这样做:
webView.loadUrl("file:///android_asset/sample.html");