Addy
Addy

Reputation: 59

execute adb shell command at runtime from the android application

In my application I want to create a directory xyz in sdcard at the runtime from the my Application.

But it doesn't work.

Here is my code..

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

}
}

Upvotes: 0

Views: 5764

Answers (2)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use below code for make directory in sdcard.

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

and add below uses-permission to android manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 0

Jim Blackler
Jim Blackler

Reputation: 23169

I've no idea if you can exec() scripts in Android, I strongly suspect you can't.

You don't need to to make a directory anyway. Do this:

new File("/sdcard/xyz").mkdirs();

Upvotes: 1

Related Questions