Android Question [Solved] Content Chooser - earliest Android version?

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

A question more for curiosity than a problem.

With the B4X TextEditor [on my Galaxy A70 Android 11] the Load button brings up the ContentChooser/FileHandler screen and all works wonderfully.
On older devices running Android 5 and 8 the Load button brings up a choice of a File Manager and external memory locations. [DropBox, GDrive etc.]
Obviously the File Manager etc does not return the "Filename" in the correct format and the Load fails.

1. I assume that ContentChooser is not part of these earlier Androids. [I thought it went back further.]
2. Can someone please pinpoint the earliest Android version that contains ContentChooser?

It's no big deal one way or the other but could be a question for the Anywhere Software trivia night.


Regards Roger
 
Last edited:

agraham

Expert
Licensed User
Longtime User
As I understand it ContentChooser uses the android.intent.action.GET_CONTENT Intent with the android.intent.category.OPENABLE and a category and a mime type and these have been in Android since the Universe was created. Note that the returned Dir and Filename are not necessarily an actual File path as noted in the Phone library.
Note that these values may point to resources other than regular files. Still you can pass them to methods that expect Dir and FileName.
Only content types that can be opened with an InputStream are supported.
I would have expected that the File Manager would work, but I only have Android 11 and 10 devices to test. On them using B4A TextEditor I can successfully open files using the Cx File Explorer, Ghost Commander and X-plore file managers. Both 10 and 11 bring up a choice of several file managers and external memory locations so maybe the Galaxy A70 has a custom chooser. I can't explain the failure on earlier devices.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User

Thanks agraham,
How do you keep all that stuff in your head.

Below, what is returned from the two devices.

HLR Modified:
Private Sub HandleLoadResult(Result As LoadResult)
    
'*** Added line to Log Result.   
Log("Result = " & Result)
    
    
    
    If Result.Success Then
        Dim s As String
        Try
            s = File.ReadString(Result.Dir, Result.FileName)
            toast.Show($"File '${Result.RealName}' loaded"$)
        Catch
            s = "Error loading file"
            Log(LastException)
        End Try
        txtField.Text = s
    End If
End Sub


From Galaxy A70 Android 11



From Galaxy Note 3 Android 5 via "File Manager" results in Error loading file.



Mystery!

Regards Roger
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is indeed a confusing issue here.
ContentChooser is supported by all Android version.

The issue is related to FileProvider and the ever-changing storage restrictions.
Starting from Android 7+ (API 24), well behaving apps, return a content:// url, which your app can access without any permission.
On older versions you get a plain old file path, however for your app to be able to access it, you need to request the READ_EXTERNAL_STORAGE permission.
 
Upvote 1

Roger Daley

Well-Known Member
Licensed User
Longtime User

Erel thanks for clearing up where CC and FP fit in. It makes things clearer.

Regards Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…