0

I am trying to create a directory in /data folder in a Rooted android device using application. I am trying two ways, first by using mkdirs() method, and other by executing the Runtime.exec(). Following is the code :

String path = "/data/abc";
File abc = new File(path);
Process proc = runtime.exec("chmod 777 /data");
proc.waitFor();
proc = runtime.exec("mkdir " + path);

Of course, if I go to 'adb shell' and simply run the command

#mkdir /data/abc

It works. But How do I do it via application. Following is the other way -

String path = "/data/abc";
File abc = new File(path);
boolean isDirCreated = abc.mkdirs();

I am not getting the required folder created in /data folder. I already have the permission in Manifest file of writing external storage.

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

Kindly suggest any way to do so.

darthvading
  • 909
  • 2
  • 11
  • 25
  • Does mkdirs() throw any exception? – WojciechKo Jul 03 '14 at 09:56
  • Did you give root permissions to your app? – m0skit0 Jul 03 '14 at 09:58
  • @WojciechKo No, mkdirs() doesn't throw anything, it just returns false. Well the javadoc of mkdirs() also doesn't throw anything, it returns 'boolean'. – darthvading Jul 03 '14 at 09:59
  • possible duplicate of http://stackoverflow.com/questions/2130932/how-to-create-directory-automatically-on-sd-card – codeMan Jul 03 '14 at 10:02
  • @m0skit0 How do I give root permission to an android app? – darthvading Jul 03 '14 at 10:02
  • @codeMan As per the reference, I am getting an issue while running 'wallpaperDirectory.mkdirs();' It is returning false. – darthvading Jul 03 '14 at 10:07
  • @ darthvading File.mkdirs() will only return true once: when the directories actually had to be created. From the documentation: returns true if the necessary directories have been created, false if the target directory already exists or one of the directories can not be created. – codeMan Jul 03 '14 at 10:12
  • @darthvading Aslo.. is there any specific reason for not using the /sdcard for creating the folder? – codeMan Jul 03 '14 at 10:13
  • @codeMan Well I have already been through the documentation of mkdirs(), it returns true if and only if the directory was created, along with all necessary parent directories; false otherwise. – darthvading Jul 03 '14 at 10:16
  • @darthvading are your sure the directory already does not exists? – codeMan Jul 03 '14 at 10:17
  • @codeMan I am specifically interested in /data folder, coz currently I am playing with some Android OS features. But this issue(seemed so minor in the beginning) is becoming quite a headache. – darthvading Jul 03 '14 at 10:18
  • @codeMan yes, that directory is definitely not existing already, that was the most obvious thing I was expecting to happen. Anyway, if it were existing, my code would have been working smoothly, as I was not monitoring the 'false' earlier. – darthvading Jul 03 '14 at 10:22
  • WRITE_EXTERNAL_STORAGE permission is not needed as /data is clearly internal memory. And if your app does not request root permissions first then it will never work. – greenapps Jul 03 '14 at 10:31
  • @greenapps How does an app request root permissions? I already tried executing the 'proc = runtime.exec("su mkdir " + path);' command. It too didn't work. What am I missing here? – darthvading Jul 03 '14 at 10:36
  • Just search/google for "requesting root access in your app". It is not trivial though. – greenapps Jul 03 '14 at 10:39

1 Answers1

0

According to this question: How to create directory automatically on SD card

Maybe you need to add permission:

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

Update:

From this question: Create a folder and write into it in the root of android
Not sure if it's 100% correct but should work:

String path = "/data/abc";
String cmd = "mkdir " + path;
Process p = Runtime.getRuntime().exec("su");
OutputStream os = p.getOutputStream();
os.write(cmd.getBytes());
os.flush();
p.waitFor();
Community
  • 1
  • 1
WojciechKo
  • 1,511
  • 18
  • 35
  • @WojciechKo Does the phone have to be rooted to execute the above code? – codeMan Jul 03 '14 at 10:16
  • @codeMan I guess so. Without root, `su` authentication should fail. – WojciechKo Jul 03 '14 at 10:22
  • @WojciechKo I tried the "su" version too now, not helpful. – darthvading Jul 03 '14 at 10:26
  • @darthvading As WojciechKo has mentioned, You need root permissions to create a folder in the /data/ directory, which you might not have, instead, if useful, you can create the directory in /sdcard. – codeMan Jul 03 '14 at 10:59
  • @codeMan I tried already using 'Process p = Runtime.getRuntime().exec("su");' It didn't work. I have to create directory in '/data/' not in '/sdcard/' – darthvading Jul 03 '14 at 11:07