Thomas Vos
Thomas Vos

Reputation: 12581

Root access with java.io.file on Android

I am trying to create a root file browser, however, I have some problems accessing root directories. When I try to access /data, the folder is empty.

When I do this:

File file = new File("/data/");

And then request for the items in that directory, it's empty. (No root access). I know how to execute simple root commands by using a Process, but then it won't work on phones without root. So I need something that will work on all phones.

I thought about using File for unrooted devices and the command ls for rooted devices, but don't know if that's the best solution. I could also just use ls with or without root, but I'd like to use File.

Is there a way to make a root file browser, while also keeping support for non-rooted phones?

Thanks

Upvotes: 9

Views: 2319

Answers (1)

Malcolm
Malcolm

Reputation: 41498

Your suggestion is correct. You cannot really use File on rooted devices for accessing the folders unavailable without root, so you'll definitely have to rely on ls in that case. Basically you have a choice between using ls everywhere and using an abstraction which will hide the details. In the latter case you will have a File-like interface which will use either File, or ls underneath.

I remember working on the same problem when I was designing my own file browser, and I opted for the second solution. It is faster to use File, so this solution has some performance advantages. I also had to write my own ls because I didn't want to rely on the one provided by the system as there are no guarantees on the output it provides.

I also suggest reading How-To SU, it has a lot of useful details on how to call the commands correctly.

Upvotes: 4

Related Questions