Android Question Data Directory Access problem

Richard Peck

Member
Licensed User
Longtime User
I'm new to B4A and I'm trying to access the data directory. I've been able to give the program superuser permission but I'm still getting errors when I read the data directory. I'm using the following code "fileList = File.ListFiles ("//data/")" to read in the directory, but I'm get errors. The code works fine for other directories. Is there a special command in need to execute or is there some special procedure I need to implement. I've been searching for a week now for a solution. Any help will be greatly appreciated.
 

eps

Expert
Licensed User
Longtime User
What are you trying to achieve?

Are you trying to access files which are part of the App itself or external files held on your device?
 
Upvote 0

Richard Peck

Member
Licensed User
Longtime User
I'm trying to display the contents of the directory, something similar to what Total Commander does. My ultimate goal is to be able to display any folder in the system. Any though of why I'm get a error.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
But you'll need to give it a location, something like :

File.Combine (Dir As String, FileName As String)

Actually what errors are you getting? Some directories will not be readily accessible, but without knowing what the errors are it's a bit hard to say what the issue is here. Saying that, it looks to me like your code just doesn't know the location of the directory you are asking it about.
 
Upvote 0

Richard Peck

Member
Licensed User
Longtime User
All I'm trying to do is list the contents of a directory. the code I'm using is:
Sub GetDir2(Directory As String )
Dim fileList As List
Dim i As Int
Try
fileList = File.ListFiles (Directory) ' =====> Reads Root directory
fileList.Sort (True)
For i = 0 To fileList.Size-1
file1 = fileList.Get(i)
If file1 <> "" Then
ListView1.AddSingleLine (" " & file1)
End If
Next
ListView1.AddSingleLine (" Number of Files = " & fileList.Size)
fileList.Sort (True)
Catch
Log(LastException)
End Try
End Sub
If the Directory used is "//" or "//acct" everything forks fine and I get a listing. If I try using "//data/" I get the follow exception:
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (List).

I know that the data directory is protected on none rooted phone, but I'm using a rooted tablet and I set the program to have superuser permissions. I've checked SuperSU and Permissions and everything says the program has superuser permissions. Is the something else I need to do to access protected directories.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Is it possible that //data/ is empty and File.ListFiles (Directory) is returning a null or uninitialized list?

Just checked an empty directory still returns an initialized list.
 
Last edited:
Upvote 0

eps

Expert
Licensed User
Longtime User
Is it possible that //data/ is empty and File.ListFiles (Directory) is returning a null or uninitialized list?

Just checked an empty directory still returns an initialized list.

No, the error is that the List should first be Initialised.

e.g. fileList.Initialize

In the code snippet provided I can't see that it is Initialised, so this needs to be addressed first or the code placed in code tags which make it easier to read.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Files.ListFiles will return an initialized list if it doesn't fail.

When trying to access //data/ it fails and returns an uninitialized list. Even if you do:

B4X:
    Dim List1 As List
    List1.Initialize
    List1=File.ListFiles("//data/")
    Log(List1)

You get the same message as an uninitialized list is returned and the Variable List1 is pointing at that.

It would seem that even though Log(File.Exists("","//data/")) returns true, File.Listfiles cannot look inside.
 
Upvote 0

Richard Peck

Member
Licensed User
Longtime User
The problem is in the "fileList = File.ListFiles (Directory)" line of code. I tried the Filelist.Initialize but it didn't change anything. For some reason the system is saying that the directory is protected and program doesn't have read access. If I use a file manager (like Total Commander) I can see into the data directory and view everything that's there. So the real question is how do you read a protected directory even when the program has superuser permission ?
 
Upvote 0

eps

Expert
Licensed User
Longtime User

So what is the exact error you get once you Initialise the File?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Android doesn't want you to be able to do that, or they would have provided a manifest permission to allow it. That said, having looked on google some have had some luck using shell commands have a look at this.

I have tried this on my tablet with super user, it works fine.
 
Last edited:
Reactions: eps
Upvote 0

Richard Peck

Member
Licensed User
Longtime User
I believe the program is registered as a superuser. I added the following line to the Manifest:
"AddPermission(android.permission.ACCESS_SUPERUSER)"
And I registered the program with the following code at startup:
---------------------------------------------------------------
Try
Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Result As Int
Dim Ph As Phone

StdOut.Initialize
StdErr.Initialize
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
File.WriteString(File.DirInternalCache, "command", "modprobe cifs" & CRLF & "modprobe aufs" & CRLF & "exit")
Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)

If StdOut.tostring <> "" Then
Results.text = "Problems found in execution"
Else
Results.text = "Test Completed OK"
End If
Catch
Results.text = LastException
Log(LastException)
End Try
---------------------------------------------------------------
After the program started, I checked in SuperSU and it was registered. I also the permissions and it had superuser permission. I know it's possible to see the directory since other file managers is it. Any other thoughts from anyone ?

I'll be trying to use the shell command to get the directory, but it will be a lot more complicated since the "LS" command returns a lot of info.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
When I tried it on my tablet, "ls" just returned the filenames "ls -l" returned more data, so it shouldn't be that difficult.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…