Android Question How to access Android/data on Android 11?

Thu Ya

Member
Now I am trying to access Android/data folder on Android 11 for reading and writing.
But I can't even list the Android folder.

Some app can access this with somehow I don't know.
Like X-Plore.(I don't need to install it from Google Play Store but it's working without any problem)

I found an answer from StackOverFlow and I have no idea with these code.

Can anyone please explain me how can I access Android/data on Android 11?

Here's my code!

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#AdditionalJar: androidx.legacy:legacy-support-core-utils

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private Storage As ExternalStorage
    Private rp As RuntimePermissions
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim b As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        MsgboxAsync("I need a permission!","Please,Tap Allow")
        Wait For Msgbox_Result
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult(Permission As String,Result As Boolean)
        If Not(Result) Then
            ExitApplication
        End If
    End If
    
    b.Initialize("ALLOW")
    Activity.AddView(b,0,0,100%x,10%y)
    b.Text = "Allow Android Folder"
    If FirstTime Then
        Storage.Initialize (Me, "Storage")
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub ALLOW_Click
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable
    Log(Storage.ListFiles(Storage.Root))
    ToastMessageShow("OK",False)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Thu Ya

Member
First of all, sorry for late reply.

But it doesn't fix my problem and I'm trying to figure out what the problem is.
So I try multiple options and I didn't get it yet.

Please light me to fix it.

It doesn't show the data folder in the Android as I say above.

Check my problem at below.

NotWorking.JPG
 
Upvote 0

Thu Ya

Member
If the TargetPhoneAPI and TargetSdkVersion is 28 or 29 then I can access Android/data folder easily.
But If the TargetPhoneAPI is 30 then I can't access it though I even set targetSdkVersion to 28, 29 or 30.

Because My app works perfectly on Android 10(API29).
Now Google update the Storage security on Android11(API30) and My app is not working well.

That's my problem.
I'm sorry about my BAD english.

Thanks,
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You should be able to access this folder with ExternalStorage.
I don't think so now. It seems to have been tightened up. You don't seem to be able to get to Root or Android/data any more. You can't even get to Android/data with the MANAGE_EXTERNAL_STORAGE permission
Storage Access Framework (SAF) is indeed one option that allows the user to grant access to directories and files. However, note that there are access restrictions to certain directories, such as the root and Android/data directories. While the majority of apps that need storage access can use best practices such as SAF or Media Store API, there could be cases where apps need broad access to shared storage or can’t do so efficiently with these best practices. For these cases, we have added the MANAGE_EXTERNAL_STORAGE permission to give access to all files on external storage, except the Android/data and Android/obb directories. To learn more about related Google Play guidelines, read the updated policy from the Policy Help Center.
Android 11 storage FAQ. First introduced in Android 10, scoped… | by nandana | Android Developers | Medium

ALSO: You can't get to Download with ExternalStorage either. Same source
Apps that target Android 11 (API level 30) and use Storage Access Framework will no longer be able to grant access to directories, such as the root directory of the SD card and the Download directory.
 
Upvote 0

Thu Ya

Member
I don't think so now. It seems to have been tightened up. You don't seem to be able to get to Root or Android/data any more. You can't even get to Android/data with the MANAGE_EXTERNAL_STORAGE permission

Android 11 storage FAQ. First introduced in Android 10, scoped… | by nandana | Android Developers | Medium

ALSO: You can't get to Download with ExternalStorage either. Same source

Thank you.
Now we are clear what the error is.
I mean yes we can't access it normally but somehow we can access it like X-Plore File Manager app.

I just want to know how this app can access it?
 
Upvote 0

Thu Ya

Member
You are correct. This is indeed a new restriction in Android 11. Working with the secondary / external storage is becoming more and more difficult...
Thank you.
If you can create a b4a lib to alternatively access these directories , Please I kindly want to request you to create.
This(StackOverFlow Answer) is the explanation of how this app can access them.
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
My problem is I no longer have access to my database from other DB manipulation programs for when I want to check if records have been created or change the data up a bit.
I'm thinking I will need a testing flag so my app knows to use the content chooser to save my DB file into the wild while I am testing.
Adapt or die, I guess, nothing new
 
Upvote 0

Thu Ya

Member
I'm writing a game injector app and that app needs access on Android/data/game/file/* folder on Android11 with API30.
So I need it and I don't know how to do with B4a.
Please Erel, let me comment my Search on this thread.

If anyone found the solution please let me know.

This is how Kotlin can access this and I don't know how to covert it to B4a.
On this StackOverFlow thread I found a sample Project and It's working.

Sorry Erel,I'm coding with B4a so long and it's hard to change Kotlin now for me.
That's why I'm asking.

Thanks,
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It looks to me they are using Storage Access Framework to access the Android folder. You can try ExternalStorage and get the user to select the Android folder, on API30 they won't be allowed access to root. Then check the granted URI is the correct one before using it. I don't know if it will work but it might. If it does, like noted in the thread, Google may regard this as a bug and patch it later.
(2) ExternalStorage - Access SD cards and USB sticks | B4X Programming Forum
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I'm writing a game injector app and that app needs access on Android/data/game/file/* folder on Android11 with API30.
So I need it and I don't know how to do with B4a.
Please Erel, let me comment my Search on this thread.

If anyone found the solution please let me know.

This is how Kotlin can access this and I don't know how to covert it to B4a.
On this StackOverFlow thread I found a sample Project and It's working.

Sorry Erel,I'm coding with B4a so long and it's hard to change Kotlin now for me.
That's why I'm asking.

Thanks,
Did you solve this?
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a lost fight. Google doesn't want apps to access the data folders of other apps.
Even if this code works and even if it won't cause your app to be rejected then Google will likely close this hole in the next update.

The upside is that there are very few cases where it is actually needed.
 
Upvote 0
Top