Android Question Intents and "Share with..."

StefanoAccorsi

Member
Licensed User
Longtime User
Hi friends. I searched a lot in the forum but couldn't find a working example.

The need is trivial: I want to manage images that different apps share with mine (through the usual menu "share with...")

I found very few posts on the argument, and most of them worked on very old Android versions.

Anyway, matching info found in this forum with that found on the Android developer pages I developed this simple app, but it doesn't work: I can't find it in "share with..." menu when I try to share an image found in the gallery.

Manifest:
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: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddActivityText(Starter,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <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.

Simple code (Starter service)
B4X:
Sub Service_Start (StartingIntent As Intent)
    Log(StartingIntent.Action)
End Sub

Thank you.
 

DonManfred

Expert
Licensed User
Longtime User
I´m not sure but i guess you need to add an activity, not an service for the intent-filter.

B4X:
AddActivityText(Sharefilter,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
will start your Sharefilter activity


For more info about intent filters look at the documentation
https://developer.android.com/guide/components/intents-filters.html
 
Upvote 0

StefanoAccorsi

Member
Licensed User
Longtime User
Hi @DonManfred, you're right: if I change the module used in the manifest editor from a service to an activity, I see my app in the "share with..." menu.

But, in the previous example, I used a service since I tried to modify this example to my needs. But, above all, because the Service_Start had the StartingIntent argument, that I could use to get the shared image. How could I do that in a standard activity?

So, I read the https://developer.android.com/guide/components/intents-filters.html page. I found that in Java they use the onActivityResult method. Searching the forum for this b4a implementation (BTW: not found) I read that the Intent is found in Activity.GetStartingIntent. So I tried:

B4X:
Sub Activity_Resume
    Dim In As Intent
    In = Activity.GetStartingIntent
    Log(In.Action)
    Log(In.HasExtra("result"))
    Log(In.GetData)
    Log(In.GetExtra("data"))
End Sub

And when sharing an image I get:

android.intent.action.SEND
false
null
null

So, this is the right way? But where I can find the image shared from the other app? Thank you.
 
Upvote 0

StefanoAccorsi

Member
Licensed User
Longtime User
It's always a great thing when I can answer myself :)

The error was thinking that the other app should have shared a "stream of bytes" to let my app have the image.

This is what I done:

B4X:
Sub Activity_Resume
    Dim In As Intent
    Dim sf As StringFunctions
    sf.Initialize
    In = Activity.GetStartingIntent
    Log("Extras=" & In.ExtrasToString)
    If In.Action = In.ACTION_SEND Then
        If In.ExtrasToString.Contains("android.intent.extra.STREAM") Then
            Dim i As Int
            i = In.ExtrasToString.IndexOf("content://")
            Dim s As String = In.ExtrasToString.SubString(i)
            s = sf.Left(s, s.Length - 2)
            GetImage(s)
        End If
    End If
End Sub

As you can imagine, when someone try to share an image, the action of the StartingIntent is set to "ACTION_SEND" and in the Extras i find "android.intent.extra.STREAM", followed by the URI of the image.

Then, I transform the URI in a simple path using this sub.

B4X:
Sub GetImage(path As String)
    Dim Cursor1 As Cursor
    Dim Uri1 As Uri
    Dim Proj() As String = Array As String("_data")
    Dim cr As ContentResolver
    cr.Initialize("")
    Uri1.Parse(path)
    Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
    Cursor1.Position = 0
    Dim res As String
    res = Cursor1.GetString("_data")
    Cursor1.Close
    Log("res=" & res)
    Dim files() As String = SplitFullPath(res)
    img.Bitmap = LoadBitmap(files(0), files(1))
End Sub

Sub SplitFullPath(s As String) As String()
    Dim sf As StringFunctions
    sf.Initialize
    Dim spl As List = sf.Split(s, "/")
    Dim path(2) As String
    path(1) = spl.get(spl.Size - 1)
    Dim rIndex As Int = s.IndexOf(path(1))
    path(0) = sf.Left(s, rIndex)
    Return path
End Sub

I don't know if this is the right way to manage it but ... it works. I'm open to advices and corrections.
 
Upvote 0
Top