Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding action visible only in specific applications to ACTION_SEND?

I'm using:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpeg");
(...)

to share image generated in my app. I would like to add custom action (save image to gallery) to intent created by

Intent.createChooser(i, "...");

I was thinking about adding activity with intent-filter for android.intent.action.SEND action, but this will make my activity visible and available to all applications. I could change setType("image/jpeg") to setType("image/*") and add

<data android:mimeType="image/foobar">

to intent-filter, but this will make my activity visible to all applications that asks for image/*.

Is there any way to filter action visibility by caller package name (or something else, that could distinguish my application from other)?

like image 541
Michał Kowalczuk Avatar asked Nov 05 '22 08:11

Michał Kowalczuk


1 Answers

Android has a nice solution for this requirement, the trick is Intent.EXTRA_INITIAL_INTENTS:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");

List<Intent> myAddedIntents = new ArrayList<Intent>();
Intent myIntent = new Intent(...);
myAddedIntents.add(myIntent);

Intent chooserIntent = Intent.createChooser(intent, "Send via:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
        myAddedIntents.toArray(new Parcelable[] {}));

startActivity(chooserIntent);
like image 102
marmor Avatar answered Nov 11 '22 09:11

marmor