Android Example Handling intents for images selected via a 'Share' menu

Thanks for your help Erel!!! I greatly appreciate your timely assistance, this active forum, the B4A community, and B4A itself!!! Gregg

Here's the Manifest file that incorporates two Intents needed to be included in Share Via menus for both single image and multiple image selections:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
'GGH 01-04-14, Intents to get included in Share Via menus
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>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

Here's a B4A code example that handles the two Intents needed to be included in Share Via menus for both single image and multiple image selections:
B4X:
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim Intent1 As Intent
    Intent1 = Activity.GetStartingIntent
    If Intent1.Action = "android.intent.action.SEND" Then
        Log("Invoked by Share Via - single image")
    Else If Intent1.Action = "android.intent.action.SEND_MULTIPLE" Then
        Log("Invoked by Share Via - multiple images")
    Else If Intent1.Action = "android.intent.action.MAIN" Then
        Log("Invoked by Click on App")
    Else
        Log("Invoked by unknown method")
    End If
    Log("Action  :"&Intent1.Action)
    Log("GetData :"&Intent1.GetData)
    Log("GetExtra:"&Intent1.GetExtra("android.intent.extra.TEXT"))
    Log("ExtraStr:"&Intent1.ExtrasToString)
   
    If  Intent1.HasExtra("android.intent.extra.STREAM") Then
        Dim i, j As Int
        Dim str, fn As String
       
        str = Intent1.ExtrasToString.ToLowerCase
        If str.EndsWith("]}]") Then
            str=str.Replace("]}]",",")
        Else If str.EndsWith("}]") Then
            str=str.Replace("}]",",")
        End If
       
        'Parse Intents
        i = str.IndexOf("content:") 
        j = str.IndexOf(",")
        Do While i>0 AND j>0
            fn = GetPathFromContentResult(str.SubString2(i,j))
            Log("Filename: "&fn)
            str = str.SubString(j+1)
            i = str.IndexOf("content:") 
            j = str.IndexOf(",")
        Loop
    End If
End Sub

'GGH 01-05-13, SQL and Reflection lib
Sub GetPathFromContentResult(UriString As String) As String
   If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
   Dim Proj() As String
   Proj = Array As String("_data")
   Dim Cursor As Cursor
   Dim r As Reflector
   Dim Uri As Object
   Uri = r.RunStaticMethod("android.net.Uri", "parse", _
      Array As Object(UriString), _
      Array As String("java.lang.String"))
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   Cursor = r.RunMethod4("query", _
   Array As Object(Uri, Proj, Null, Null, Null), _
   Array As String("android.net.Uri", _
      "[Ljava.lang.String;", "java.lang.String", _
      "[Ljava.lang.String;", "java.lang.String"))
   
   Cursor.Position = 0
   Dim res As String
   res = Cursor.GetString("_data")
   Cursor.Close
   Return res
End Sub
 

pesquera

Active Member
Licensed User
Longtime User
Thank you, this was so useful to me
Just in case that someone need this:
I've changed 1 line of the code because I've received this on "str" string variable:
bundle[{from-myfiles=true, android.intent.extra.stream=content://media/external/images/media/17328, exit_on_sent=true}]
B4X:
'j = str.IndexOf(",")
j = str.IndexOf2(",",i)
 
Top