Android Tutorial Receiving shared images from other apps

Discussion in 'Tutorials & Examples' started by Erel, Jun 26, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Code:
    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:
    Code:
    #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"uri100%x100%y)
           
    Activity.SetBackgroundImage(bmp)
         
    Catch
           
    Log(LastException)
         
    End Try
       
    End If
    End Sub

    Private Sub IsRelevantIntent(in As IntentAs 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.
     
    pedrocam, MarkusR, ellpopeb4a and 8 others like this.
  2. Tadeu Botelho

    Tadeu Botelho Member Licensed 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.

    Code:
    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"uri100%x100%y)
                
    'Activity.SetBackgroundImage(bmpImagemExternaViaIntent)
            Catch
                
    Log(LastException)
            
    End Try
    End If
    Thank you
     
    Last edited: Mar 5, 2018
  3. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    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: Mar 6, 2018
  5. Erel

    Erel Administrator Staff Member Licensed User

    That's true. The sharing intent must be intercepted by the activity.
     
  6. dcoun

    dcoun Member Licensed 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?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    This example shows how you can receive a single image. If you have any other question then you should start a new thread.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice