Android Example Retrieve one or multiple image(s) shared to your app

KMatle

Expert
Licensed User
With this code you can retrieve images shared to your app (e.g. if a user selects one or more images from the gallery and selects "Share..."). No permissions are required as the user selects the images :)

Add this to the manifest (send = one image, multiple = multiple images)

*code optimized*

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>)
Code

B4X:
Dim ImageList As List
ImageList=GetUrisFromStartingIntent(Activity.GetStartingIntent)
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

With friendly regards to @DonManfred
 
Last edited:
Top