0

I am trying to use SuperUser commands to create a list of files that are in a certain location. I am using the method laid out in this post:

Android using Super User Permissions ? allowing access

My specific code looks like this:

try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); 
            DataInputStream inputStream = new DataInputStream(process.getInputStream());

            outputStream.writeBytes("cd " + baseDirectory + "/system/app" + "\n");
            outputStream.flush();

            outputStream.writeBytes("ls" + "\n");
            outputStream.flush();

            outputStream.writeBytes("exit\n"); 
            outputStream.flush(); 
            process.waitFor();
        }  catch (IOException e) {

        }  catch (InterruptedException e) {

        }

and it runs without any errors. My problem is that I can't figure out how to produce any output.

Please note that in the code I am trying to get a list of Apps (I know I can do this in different ways) but I need it to work in a general case...

Community
  • 1
  • 1
easycheese
  • 5,859
  • 10
  • 53
  • 87

2 Answers2

0

After you flush the exit command try reading your DataInputStream:

BufferedReader reader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
    output.append(buffer, 0, read);
}
String result = output.toString();
JonnyBoy
  • 1,555
  • 14
  • 24
0

You don't need to use su to get a list of installed packages

How to get a list of installed android applications and pick one to run

However in your code, you say it doesn't output anything. But what exactly would you like it to output (and where)? You never told it to display anything.

Community
  • 1
  • 1
Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • I don't think you understand my question. I am trying to get a list of files, not necessarily installed packages and since I am accessing protected files, I do need to use SU. Second, I know I didn't output anything, that's what I was asking help to do. I can't figure out how to produce the output (say in a list) from the code above. – easycheese May 25 '12 at 14:42