Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 840
Thomas Vos Avatar asked Mar 04 '16 09:03

Thomas Vos


People also ask

How do I set root permission on Android?

In most versions of Android, that goes like this: Head to Settings, tap Security, scroll down to Unknown Sources and toggle the switch to the on position. Now you can install KingoRoot. Then run the app, tap One Click Root, and cross your fingers. If all goes well, your device should be rooted within about 60 seconds.

Can Android read Java?

You can do it quite easily as there are many ways to run Java apps on Android. Specific application called Java Emulators can do it quite easily. These are the popular Java emulators for Android: viz, JBED, PhoneME, Jblend and NetMite. There are arranged in order of my preference.


1 Answers

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.

like image 177
Malcolm Avatar answered Oct 12 '22 03:10

Malcolm