Reputation: 189
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?
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
Upvotes: 1
Views: 10021
Reputation: 213
Use as following:-
Process process = Runtime.getRuntime().exec("command to be executed");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Upvotes: 0
Reputation: 57173
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.
Upvotes: 1
Reputation: 61351
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.
Upvotes: 0
Reputation: 39807
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.
Upvotes: 6
Reputation: 1373
try this :
Process p = Runtime.getRuntime().exec("cd /data");
Upvotes: 0