Executing shell command in order to write to a file through apk

188 views
Skip to first unread message

AbhiRocks

unread,
Aug 22, 2011, 3:23:19 AM8/22/11
to android-platform
Hi all,

I am trying to create an apk which would execute a shell command.
This shell command will write to a file in the shell i.e. sdcard.

The code snippet that i am using is this:

@Override
protected void onStart() {
super.onStart();

Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
public void run() {
String cmd = "ls /data >> /sdcard/date.txt";
java.lang.Process p;
try {
p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()),cmd.length());
Log.i("AbhiRocks", "Output is " + input.readLine());
} catch (IOException e) {
e.printStackTrace();
}

}
}, 5000);


}



And in the manifest.xml file i have added the line:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

But i am unable to generate any date.txt file once i run this apk.
P.S. It executes other shell commands which does not involve writing
to a file.
I know that we can write to a file using inbuilt android apk
commands but I need to write/create a file using
shell commands only.

Thanks and Regards,
AbhiRocks

Anand R

unread,
Aug 22, 2011, 2:06:13 PM8/22/11
to android-...@googlegroups.com
 
Try using the following command in your code...
 
String cmd = "ls /data > /sdcard/date.txt";
Regards,
Anand.

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Regards,
Anand.

Dianne Hackborn

unread,
Aug 22, 2011, 4:23:00 PM8/22/11
to android-...@googlegroups.com
This is about the most inefficient way to write a file that you can come up with.

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Reply all
Reply to author
Forward
0 new messages