1

As in android(through android sdk/tools folder) from command line we can execute linux shell command to access mnt folder/data folder likewise. (e.g cd data ls) now that command i want to execute from programmatically in android so how could it be possible?


I am using following code to execute shell command
Process p = Runtime.getRuntime().exec("cd data");

but it is giving me exception

java.io.IOException: Error running exec(). Command: [cd, data] Working 
Directory:  null Environment: null


so how should i proceed for it.Thanks in advance.
kaiz.net
  • 1,984
  • 3
  • 23
  • 31
AndroidDev
  • 189
  • 1
  • 2
  • 7

5 Answers5

6

cd is not a Linux command, it's a command built into the shell; it changes the current working directory in the context of that shell process. In your case, if the command were to be successful, it would be successful for the child process only (which would soon terminate) and would have no effect on your own process.

mah
  • 39,056
  • 9
  • 76
  • 93
  • I hasn' got what you want to say exactly – AndroidDev Sep 06 '12 at 12:59
  • What I'm saying is that executing a "cd" command is not going to have any effect on you. The reason for this is Runtime()...exec() will create a child process; even if you could get 'cd' to work in it, it would not affect your Android process so it would be useless to your Android app. – mah Sep 06 '12 at 13:10
  • ok i got it now but can you please help me to provide any solution to my problem? – AndroidDev Sep 06 '12 at 13:32
  • I've done a little Googling to see if there is a Java chdir() type function and I haven't found much of use. One possibility (an ugly one, much more difficult than it is worth) is to create a native function which calls the C chdir() function. You would probably be much better off though just appending the full path to file names you refer to -- then it would not matter what your current directory is. – mah Sep 06 '12 at 13:40
  • ok this is my full path(data/data/androidCode) so how should i insert in my code to retrieve files from that folder? – AndroidDev Sep 06 '12 at 13:53
  • If trying to access files that are private to you (which your path sort of suggests, though `androidCode` is not really a well formed package name), you should use Android's built-in library. When doing this for your internal files, this lets you avoid worrying about the absolute path. See http://developer.android.com/guide/topics/data/data-storage.html for information, primarily the Internal Storage section. – mah Sep 06 '12 at 13:58
  • you can consider (data/data/com.activity.networkRequestDetector)as my absolute path but please tell me how to access it? – AndroidDev Sep 06 '12 at 14:03
  • The link I gave you has best case practices, which will survive a change of package name or even a change in how the OS does things (currently Android uses /data/data/_packagename_ however you have no guarantee that it will never change; their API calls immunize you from that). If you insist on hard coding the path though, simply prepend the absolute path to your file names... such as String basePath="/data/data/com.activity.networkRequestDetector"; BufferedReader in = new BufferedReader(new FileReader(basePath + "filename.txt")); – mah Sep 06 '12 at 17:31
  • I tried as per your suggestion but it is giving me file not found exception cause by permission denied. – AndroidDev Sep 07 '12 at 07:15
1

On Android, your process does not have permissions to read files in other app's /data/data/_other-package-name_, or list its private files in directory /data/data/_other-package-name_/files. But it does have permission to list and read files in the lib directory /data/data/_other-package-name_/lib, and you can look at a specific file in /data/data/_other-package-name_/files, if the other-package opened this file as public.

I.e. if the other-package does something in line with:

FileOutputStream fos = openFileOutput("public_file", Context.MODE_WORLD_READABLE);
fos.write("hello world".getBytes());
fos.close();

then your package can read this file like this:

byte[] bytes = new byte[100];
FileInputStream fis = new FileInputStream(new File("/data/data/*other-package*/files/public_file"));
int cnt = fis.read(bytes);
fis.close();
Log.d("Two_Libs", new String(bytes, 0, cnt));

But you cannot list the public files in that directory to discover them.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

try this :

Process p = Runtime.getRuntime().exec("cd /data");
Aditya Nikhade
  • 1,373
  • 10
  • 15
  • If the command were almost anything except 'cd' your answer would be good. For cd specifically however, it simply will not work as intended (even if there were an external cd binary -- though there is not). – mah Sep 06 '12 at 12:55
  • Aditya, the only thing you've told him to do is change the directory he changes into -- but that doesn't address the underlying reality that changing directory from within a child process has no effect on the parent; thus your answer can not actually help him in this case. – mah Sep 06 '12 at 13:14
0

To retrieve the path to your app's private data folder use the following from Java:

File MyData = Ctxt.getDir("Foo");

Where Ctxt is a Context object, like an Activity. It will return you a path like /data/data/com.activity.networkRequestDetector/app_Foo. Note that reading/writing /data/data/com.activity.networkRequestDetector/ is discouraged in Android - it's your application's sandbox's root, not to be played with.

To open files from the data folder, use something like this:

FileInputStream Stm = new FileInputStream(new File(MyData, "Filename.txt"));

In general, anything a shell command does your app can do, too. Shell commands are just programs that use API like everyone else.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • ya i know it but when i want to another application data from my application then it will not work – AndroidDev Sep 06 '12 at 14:30
  • On a non-rooted device it won't work anyway. Other apps' data is protected from yours by OS-level permissions. On a rooted device, just construct a File object around an absoluite path. Seriously, you don't need shell commands. – Seva Alekseyev Sep 06 '12 at 15:09
0

Use as following:-

 Process process = Runtime.getRuntime().exec("command to be executed");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Parveen Kumar
  • 175
  • 3
  • 5