Android Question Basic question: How to load photo from the DCIM directory with SDK 30?

AndrewKing

Member
Licensed User
Longtime User
The test code is very simple:
B4X:
    Dim TmpBmp As Bitmap
    TmpBmp = LoadBitmap("/storage/emulated/0/DCIM/","1.jpg")

It doesn't work if you set targetSdkVersion="30":



This is the manifest:
 

AndrewKing

Member
Licensed User
Longtime User
Please post text as text. Not screenshot.

1. Read: android.jar / targetSdkVersion / minSdkVersion
2. Correct way to let the use choose an image is with ContentChooser.

Thank you very much for your answer.

> 1. Read: android.jar / targetSdkVersion / minSdkVersion

I've read it. I know how to use "requestLegacyExternalStorage" to resolve it when targetSdkVersion="29". But it doesn't work for SDK 30.

> 2. Correct way to let the use choose an image is with ContentChooser.

1) ContentChooser can only select 1 file at a time. If you need to load multiple files at a time you can not use ContentChooser.
2) If I already know the location of the image file, there is no need to ask the user to choose it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If I already know the location of the image file, there is no need to ask the user to choose it.
Google doesn't agree with you.

You can use ExternalStorage to get a permanent access to the folder, but it will require the user to correctly select the folder once.
 
Upvote 0

AndrewKing

Member
Licensed User
Longtime User
Google doesn't agree with you.

You can use ExternalStorage to get a permanent access to the folder, but it will require the user to correctly select the folder once.

Yes you are right. The problem I have met is that ContentChooser can only select one file at a time, so I have to make a image selector by myself, which can select multiple images at a time. I used the library 'MediaBrowser' to make the selector. MediaBrowser can return all images' locations like "/storage/emulated/0/DCIM/1.jpg". After the user select images, My APP gets all selected paths. then it will load and process all selected images. Since SDK 30, It seems that you can not access those images files like before. So I come here to get help...
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You can let the user share multiple pics to your app: https://www.b4x.com/android/forum/threads/receiving-shared-images-from-other-apps.81041/

Call from Resume:

B4X:
Private Sub GetUrisFromStartingIntent(in As Intent) As List
    Log(in.Action)
    Log(in.GetData)
    Log(in.ExtrasToString)
    Dim ImageList As List
    ImageList.Initialize
    If in.IsInitialized And in <> OldIntent Then
        If in.Action = "android.intent.action.SEND" Or in.Action= "android.intent.action.SEND_MULTIPLE" Then
           ImageList= GetImageUrisFromExtras(in.ExtrasToString, in.action)     
        End If
        OldIntent = in
    End If
    Return ImageList
End Sub

Sub GetImageUrisFromExtras (Extras As String, Intent As String) As List
    Log(Extras)
    Dim ImageList As List
    ImageList.Initialize
    
    Dim JO As JavaObject = Activity.GetStartingIntent
    Select Intent
        Case "android.intent.action.SEND"
            Dim uri As String = JO.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
            ImageList.Add(uri)
        Case "android.intent.action.SEND_MULTIPLE"
            Dim uris As List = JO.RunMethod("getParcelableArrayListExtra", Array("android.intent.extra.STREAM"))
            If uris.Size > 0 Then
                For i = 0 To uris.Size-1
                    ImageList.Add(uris.Get(i))
                Next
            End If
    End Select
    
    Return ImageList
End Sub

Manifest:

B4X:
AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…