[英]execute adb shell command at runtime from the android application
在我的應用程序中,我想在運行時從我的應用程序在sdcard中創建目錄xyz。
但這是行不通的。
這是我的代碼。
public class process extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] str ={"mkdir","/sdcard/xyz"};
try {
Process ps = Runtime.getRuntime().exec(str);
try {
ps.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
}
}
}
我不知道您是否可以在Android中執行exec()腳本,我強烈懷疑您不能這樣做。
無論如何,您無需建立目錄。 做這個:
新File(“ / sdcard / xyz”)。mkdirs();
將以下代碼用於sdcard中的make目錄。
File dir = new File("/mnt/sdcard/xyz");
try{
if(dir.mkDir()) {
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}catch(Exception e){
e.printStacktrace();
}
並在Android清單文件中添加以下uses-permission。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.