Intent filter with navigation component to Receiving image data from other app

Viewed 128

My Android application uses the navigation component to navigate between fragments.

And the project only has a single Activity and all the others are fragments.

My application is a social media application. I want to share an image from my phone gallery with my application. When a user clicks the share button on the gallery, my application wants to show in the sharing dialogue.

<intent-filter>
     <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="image/*" />
    </intent-filter>

I have used this intent filter on my main activity, and then the share dialogue displays my app.

But my requirement is, I want to get that image on my fragment. How to use this intent filter on my fragment.

Can I use the deep-link for that? but what value will give on the uri section?.

<deepLink
            app:uri="android.intent.category.DEFAULT"
            app:action="android.intent.action.SEND"
            app:mimeType="image/*"
            />

I tried this. but not working.

In short, I want to receive the data (image) in my fragment. How can I achieve this using the Navigation component?

1 Answers

To explain why it's not working for you, let's examine this piece of code,

            app:uri="android.intent.category.DEFAULT"
            app:action="android.intent.action.SEND"
            app:mimeType="image/*"/>

app:uri attribute requires a scheme and a host e.g., https://example.com (it could be any made up uri like myapp://example.com) , but you are passing a category string to it. To fix this, just pass "myapp://example.com" or any made up uri you can think of. That will work. Now, the important bit - you must add deeplink tag to the fragment which receives the image. Now, your code will work, however, if your application is already running, it will not receive the image from shared intent. For that, you need to override onNewIntent method in you activity and call NavController.handleDeepLink(intent), you have to pass the intent received in onNewIntent method to it, followed by a call to super.onNewIntent(). Now, your app can receive image from any application that can share it, whether your application is already running, or started by the intent itself.

Related