Android Tutorial Receiving shared images from other apps

Status
Not open for further replies.
This example shows how to create an app that can act as a sharing target. It will be listed in the list of apps that show when the user shares an image.

The first step is to add an intent filter to the manifest editor. The intent filter tells the OS which types of resources the app supports:
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>)

The second step is to check the starting intent in Activity_Resume and see whether it is a sharing intent. If so then we extract the uri from the intent and load the bitmap. The uri scheme is expected to be content://.

As we don't want to handle the same intent twice we need to compare it to the previous intent and make sure that it is not identical:
B4X:
#BridgeLogger: True

Sub Process_Globals
   Private OldIntent As Intent
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume
   If IsRelevantIntent(Activity.GetStartingIntent) Then
     Dim in As JavaObject = Activity.GetStartingIntent
     Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
     Try
       Log("Loading bitmap from: " & uri)
       Dim bmp As Bitmap = LoadBitmapSample("ContentDir", uri, 100%x, 100%y)
       Activity.SetBackgroundImage(bmp)
     Catch
       Log(LastException)
     End Try
   End If
End Sub

Private Sub IsRelevantIntent(in As Intent) As Boolean
   If in.IsInitialized And in <> OldIntent And in.Action = in.ACTION_SEND Then
     OldIntent = in
     Return True
   End If
   Return False
End Sub

Tip: Test it in release mode. Otherwise the process might be killed while the app is in the background and it will fail to start when the image is shared.
 

Tadeu Botelho

Member
Licensed User
Longtime User
Hello
Is it possible to include this code to function as a service?
I need to receive images via "intent" in the background.
I could not make it work as a service.

B4X:
If IsRelevantIntent(Activity.GetStartingIntent) Then '// ERROR IN Service
        Dim in As JavaObject = Activity.GetStartingIntent '// ERROR IN Service
        Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
        Try
            Log("Loading bitmap from: " & uri)
            'Global Var Test:
            bmpImagemExternaViaIntent = LoadBitmapSample("ContentDir", uri, 100%x, 100%y)
            'Activity.SetBackgroundImage(bmpImagemExternaViaIntent)
        Catch
            Log(LastException)
        End Try
End If

Thank you
 
Last edited:

MarkusR

Well-Known Member
Licensed User
Longtime User
Is it possible to include this code to function as a service?
i think you can give this data in activity resume your service and close the activity direct with Activity.Finish


@Erel btw this example is also very useful, thanks
 
Last edited:

dcoun

Member
Licensed User
Longtime User
Hi,
the "ACTION_SEND_MULTIPLE" is not included in the Intent object.
Am I missing something? How can I receive multiple images from an other app?
 

scottie

Member
Licensed User
Longtime User
Question: This works just fine for me when I 'share' using a file manager.
But, when I use other apps like Gallery, the URI looks like: content://media/external/images/media/84
this is not the same URI that file manger sends? There is not even a filename.

URI from file manager: (this works)
content://com.lcg.Xplore.FileContent/uid/file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDownload%2FASNAG.jpg?size=473821&time=1628783761000

URI from Gallery / other apps: (this doesn't work at all)
content://media/external/images/media/84

B4X:
    If IsRelevantIntent(Activity.GetStartingIntent) Then
        Dim in As JavaObject = Activity.GetStartingIntent
        Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
        Log("URI:"&uri)   'which is: content://media/external/images/media/84


Why the difference and how to compensate?
 
Status
Not open for further replies.
Top