-1

I'm trying to make a file browser (specifically I started changing this: http://android-er.blogspot.com.es/2010/01/implement-simple-file-explorer-in.html ).

My question is: How do I get access and see the files in the directory "/"? I found out there looking for the famous: Runtime.getRuntime().exec("su"); but I only do that superuser give permissions for admin and stop working.

I followed the steps here to root emulator: How to get root access on Android emulator? . Anyway my phone is rooted and does not work.

This is the code. The application is in superuser´s list but always get the message folder can not be read!

public class AndroidExplorer extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    List<String> cmds = new ArrayList<String>();
    cmds.add("mount -o rw,remount /system");
    try {
        doCmds(cmds);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    myPath = (TextView)findViewById(R.id.path);
    getDir(root);
}

public void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
} 

private void getDir(String dirPath)
{
 myPath.setText("Location: " + dirPath);
 item = new ArrayList<String>();
 path = new ArrayList<String>();
 File f = new File(dirPath);
 File[] files = f.listFiles();

 if(!dirPath.equals(root))
 {
  item.add(root);
  path.add(root);
  item.add("../");
  path.add(f.getParent());
 }
 for(int i=0; i < files.length; i++)
 {
   File file = files[i];
   path.add(file.getPath());
   if(file.isDirectory())
    item.add(file.getName() + "/");
   else
    item.add(file.getName());
 }
 ArrayAdapter<String> fileList =
  new ArrayAdapter<String>(this, R.layout.row, item);
 setListAdapter(fileList);
}

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  File file = new File(path.get(position));
  if (file.isDirectory())
  {
   if(file.canRead())
    getDir(path.get(position));
   else
   {
    new AlertDialog.Builder(this)
    .setIcon(R.drawable.ic_launcher)
    .setTitle("[" + file.getName() + "] folder can't be read!")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
   }
  }
  else
  {
   new AlertDialog.Builder(this)
    .setIcon(R.drawable.ic_launcher)
    .setTitle("[" + file.getName() + "]")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
  }
 }

}

Cœur
  • 37,241
  • 25
  • 195
  • 267
ObiWan
  • 1
  • 1
  • 2

1 Answers1

0

Anyway my phone is rooted and does not work.

To start with you need a working rooted phone. Rooting method depends on your phone model so you need to google it and figure it out.

Once you have rooted your phone successfully, you will have super user rights and you will be able to execute su commands.

You can check if your phone is rooted properly like this(in windows command prompt):

> adb shell
$ su

And you should be able to see the files on root folder for example like this:

$ cd /
$ ls -al

EDIT

Replace:

Runtime r = Runtime.getRuntime();
process = r.exec("su");

With the following:

List<String> cmds = new List<String>();
cmds.add("mount -o rw,remount /system");
doCmds(cmds);

And add the following function:

public void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
}  
Caner
  • 57,267
  • 35
  • 174
  • 180
  • Caner thanks for your answer!!, sorry but I have not explained well. I have a phone with Cyanogen mod, therefore it is rooted. I installed the application root explorer (v2.16) and superuser on the emulator and working properly. I have root access with rootexplorer and I can read and write to all directories. I get my application is stored in the application list of superuser (with rootexplorer) I got that by Runtime.getRuntime (). Exec ("su"), but now I can not read, copy or delete any file. – ObiWan Jul 04 '12 at 07:26
  • ok, what error do you get when you try to read/copy/delete? Try "mount -o rw,remount /system" – Caner Jul 04 '12 at 07:32
  • Hello Caner, I've edited the post. I put the code. The flow always goes the way of else of " if (file.canRead ()) ". If I delete that condition, I get a force close: "The application has stopped unespectledly. Please try again. – ObiWan Jul 04 '12 at 08:15
  • For example if I go to "/ data" with rootexplorer, I can see all the files inside. However, with this application I get the alert "folder cant be read!" – ObiWan Jul 04 '12 at 08:31
  • still does not work, continue with the message "cant read file". I edited the code of the firstpost so you can see I've changed and I've tested. – ObiWan Jul 04 '12 at 09:06
  • Lots of misinformation in this post. Please do not use root if you do not understand what you are doing. Do not gratuitously remount the system partition writable. The ability to list / is not a reliable indicator of running as root. – Chris Stratton Jul 04 '12 at 10:44
  • Hi Chris, thanks for the warning. I know i have to be careful when I work with "/" and some files, because this can interferes with the integrity of the system. I'm learning android, but I have been for some time in the world of computer :). However I am learning and nothing happens if I break something xD, I take responsibility for the damages in my stuff. You say not displaying the contents of "/ data" does not indicate run as root. How I can do this if I don´t run it as root? – ObiWan Jul 04 '12 at 11:36
  • This answer incorrectly suggests that listing "/" would indicate if you are running as root; it won't. But to list "/data" would however (if successful) indicate that your are running as a more privileged than ordinary user ID such as system or root, and since you aren't attempting to be system, that would probably mean you are root. – Chris Stratton Jul 05 '12 at 12:25