Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting the target application when sending an Intent

I am sharing image by using the send intent(ACTION_SEND).

I want to know if any application is selected for sharing or not. How can I do that and how do I know if the image was sent successfully?

Code I have used to share image is here :

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(imageSharePath)));
startActivity(Intent.createChooser(share, "Share Image"));
like image 241
Zankhna Avatar asked Dec 16 '22 06:12

Zankhna


2 Answers

You need to implement your own dialog for the activity selection.

To create such dialogs you need to use PackageManager.queryIntentActivities(). This method returns List<ResolveInfo>.

ResolveInfo contains some information about an activity (e.g. resolveInfo.activityInfo.packageName), and with the help of PackageManager you can get other information (useful for displaying the activity in the dialog) - application icon drawable, application label, etc.

Display the results in a list in a dialog (or in an activity styled as a dialog). When an item is clicked create new Intent.ACTION_SEND, add the content you want and add the package of the selected activity intent.setPackage(pkgName).

like image 103
Anchit Mittal Avatar answered Dec 17 '22 20:12

Anchit Mittal


The answer above is no longer correct.

Since API 22 it is possible to detect the target application when the user shares.

For details see:

Get IntentSender object for createChooser method in Android

https://medium.com/code-with-lisa/get-results-from-android-chooser-9cfc5445a871

like image 44
Frank Harper Avatar answered Dec 17 '22 20:12

Frank Harper