Background
I wish to show the native intent-chooser, while having the ability to customize it a bit.
For this, I've found the next StackOverflow thread:
How to customize share intent in Android?
The problem
Thing is, when I use the suggested code on Android 5.x and below, everything seems to be fine, but when I use it on Android 6.0.1 (tested on Nexus 5 and emulator, when having multiple apps to share content with) , I get empty cells and sometimes even empty app names, as such:
This doesn't appear when using the non-customized intent-chooser:
startActivity(Intent.createChooser(intent, "default chooser"));
The code
Seeing the solutions, I've created the next code:
private void test(Intent shareIntent) {
List<Intent> targetedShareIntents = new ArrayList<>();
final List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
Intent targetedShareIntent = new Intent(shareIntent);
targetedShareIntent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
startActivity(chooserIntent);
}
}
private void prepareIntentToShare(Intent intent) {
intent.setAction(android.content.Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, mUri);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "title");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "body");
}
And the way to test it:
Intent intent = new Intent();
prepareIntentToShare(intent);
test(intent);
What I've tried
I've tried to change various things in the intents, but without any luck. I've also tried to find out what is the order that the intents are supposed to be in (because maybe it's important), but I didn't find it.
Lastly, I've decided to post about it to Google, assuming this is a bug:
https://code.google.com/p/android/issues/detail?id=202693
The questions
Why does it occur? Can I fix it somehow, while still using the native intent-chooser? How come it occurs only on Android 6 and above?
How can I put the correct name for each item there, as I see "twitter" twice, for example, yet other apps do show the correct name (like the one of the qr-code-scanner)?
Is it possible to keep the native behavior of how to order of apps, as shown using the simple way of showing the intent-chooser? Maybe get the list of apps the way they are supposed to be ordered ?
I spend some time to read ChooserActivity and ResolverActivity and kind of solving thoese problems.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty()) {
List<Intent> targetIntents = new ArrayList<>();
for (ResolveInfo resolveInfo : resolveInfos) {
ActivityInfo activityInfo = resolveInfo.activityInfo;
// remove activities which packageName contains 'ttt' for example
if (activityInfo.packageName.contains("ttt")) {
continue;
}
Intent targetIntent = new Intent(Intent.ACTION_SEND);
targetIntent.setType("text/plain");
targetIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.setting_share_app_subject));
targetIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.setting_share_app_body));
targetIntent.setPackage(activityInfo.packageName);
targetIntent.setComponent(new ComponentName(activityInfo.packageName, activityInfo.name));
// wrap with LabeledIntent to show correct name and icon
LabeledIntent labeledIntent = new LabeledIntent(targetIntent, activityInfo.packageName, resolveInfo.labelRes, resolveInfo.icon);
// add filtered intent to a list
targetIntents.add(labeledIntent);
}
Intent chooserIntent;
// deal with M list seperate problem
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// create chooser with empty intent in M could fix the empty cells problem
chooserIntent = Intent.createChooser(new Intent(), context.getString(R.string.setting_share_app_title));
} else {
// create chooser with one target intent below M
chooserIntent = Intent.createChooser(targetIntents.remove(0), context.getString(R.string.setting_share_app_title));
}
if (chooserIntent == null) {
return;
}
// add initial intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[targetIntents.size()]));
try {
context.startActivity(chooserIntent);
} catch (ActivityNotFoundException e) {
Logger.e(TAG, e, e);
}
}
EDIT: seems on Android Q (10 - API 29) this is broken, and will show just up to 2 items instead of all of them. Asked about this again here.
EDIT: made a sample of choosing which items to exclude from sharing, here, which should work for all Android versions.
Related
I'm implementing custom share feature for ACTION_SEND intent. I want to exclude my own app from sharing list. To achieve this I'm getting all suitable activities using getPackageManager().queryIntentActivityOptions(...) and then checking the package.
The problem is that getPackageManager().queryIntentActivityOptions(...) returns me a list of ResolveInfo objects where priority is always 0 and apps are ordered wrong - not like in the default share dialog. Thus apps don't have a proper order in a list and it's not convenient for user.
Here is a code which does sharing:
public void shareExcludingApp(String packageNameToExclude, Uri imagePath) {
List<Intent> targetedShareIntents = new ArrayList<>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
List<ResolveInfo> resInfo = mContext.getPackageManager().queryIntentActivities(createShareIntent(imagePath), 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = createShareIntent(imagePath);
if (!info.activityInfo.packageName.equalsIgnoreCase(packageNameToExclude)) {
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), mContext.getString(R.string.choose_an_app));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
mContext.startActivity(chooserIntent);
}
}
private Intent createShareIntent(Uri uri) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
return share;
}
Am I configuring the intent wrong? Do I need to supply more data somewhere?
Thanks
Update:
Thanks to #CommonsWare I realized that I shouldn't use priority to order the list myself, instead the list should be sorted already (queryIntentActivities):
Returns a List of ResolveInfo objects containing one entry for each matching activity, ordered from best to worst.
But it's not sorted properly in my case and I'm getting activities mixed up. Can you please help me?
I want to use the directshare feature, but i need to exclude apps.
The excluding part works pretty well, i am just giving an array of intents to the chooser, while the intents are only including one specific application.
But doing this directshare does not work.
Directshare only seems to be working when giving exactly one intent to the chooser.
Is it possible to exclude apps and use directshare?
Code Snippets:
Sharing with a list of intents (How to filter specific apps for ACTION_SEND intent (and set a different text for each app)) :
final Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), "Share with: ");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
activity.startActivity(chooserIntent);
Sharing with directshare, but no excluding:
final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
activity.startActivity(Intent.createChooser(sendIntent, "Share with:"));
I ran into the same problem with Direct Share, and found that it only seems to work for the target intent passed to createChooser().
My kludgy work-around was to lookup "com.android.mms" and pass that intent to createChooser() and the others in the targetedShareIntents array, which means that at least Direct Share works for text messages.
Note for some apps, not setting the class name in targetedShareIntents means you end up with Android System appearing in chooser instead.
For me this solution isn't good enough, and I'm leaning towards not excluding my own app from the list. Hopefully my efforts will lead someone to something better.
Code below is a variation on examples found here:
Custom filtering of intent chooser based on installed Android package name
I see here: http://stackoverflow.com/a/23036439 that saulpower may have a better solution, but I can't get it to work with my UI.
private void shareExludingApp(Intent intent, String packageNameToExclude, String title) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
Intent directShare = null;
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(intent);
if (!info.activityInfo.packageName.startsWith(packageNameToExclude)) {
targetedShare.setPackage(info.activityInfo.packageName);
targetedShare.setClassName(info.activityInfo.packageName,
info.activityInfo.name);
if (directShare == null && info.activityInfo.packageName.equals("com.android.mms")) {
directShare = targetedShare;
} else {
targetedShareIntents.add(targetedShare);
}
}
}
}
if (targetedShareIntents.size() > 0) {
if (directShare == null) {
directShare = targetedShareIntents.remove(0);
}
Intent chooserIntent = Intent.createChooser(directShare, title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
else {
startActivity(Intent.createChooser(intent, title));
}
}
Usage:
shareExludingApp(intent, getPackageName(), "Share via");
I created my own file extension (.oli). If the user clicks on a file with this extension my app starts and loads the included data. This works like expected. The problem is I would like to give the user of my app the opportunity to share a file (Example: filename.oli).
I implemented this so far:
public void shareFile(){
File file = getShareableFile(); //Creates a .oli-file
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.setType("*/*"); //Maybe the problem
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name);
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareDatei)));
}
The problem is, the list of apps that think they could handle sharing my file is very large because of shareIntent.setType("/"); Here are two cases that happen if you share with different apps:
If I choose an Email-app like Gmail to share my file it works how it should. The email includes the file as filename.oli . When I click on it my app gets started.
But If I choose for example the Quickmemo-app I get a message that this file can not be shared by this.
So all in all I just want to show apps in the chooserlist that can handle to share my file with the .oli extension . How do I do that? Thanks in advance!
The setType() intent method actually uses MIME types to filter apps according to the docs. So, you'll only be able to filter the apps available based on the following filters:
1.Text
sendIntent.setType("text/plain");
2. Binary
shareIntent.setType("image/jpeg");
3. Multiple content items
shareIntent.setType("image/*");
EDIT
Here's what I would likely do, I would know which apps I want to be able to share the file, like gmail since you know that works, and I would be selective about which apps are in the list. The following code comes from the answer in this SO link: How to filter specific apps for ACTION_SEND intent (and set a different text for each app)
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
I want in my app to share multiple photos. I am able to upload one photo using facebook graph API but how can I share multiple photos
Thanx.
Android does not provide an out-of-the-box Intent for selecting multiple images/pictures, or any other media type. Source: https://stackoverflow.com/a/12919585/450534 (And I usually will take Mark Murphy's word as Gospel unless someone can challenge it)
The closest Intent is the ACTION_SEND_MULTIPLE. That. however, is not an option for you.
You will need to create a custom selector similar to how Facebook does in it's own mobile app.
You will get a full functioning example for implementing your own multiple image selector here: http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/
And finally, to upload multiple image to Facebook in one go, you will need to send Batch Requests.
But there certainly is no ready made solution for what you are looking for for. Combine all the above, and then you will. But nothing straightforward I'm afraid.
I managed to share multiple photos in Facebook using intents.
The variable "caminhos" is an ArrayList < String > with the paths of the images you want to share.
protected void share(String nameApp, String imagePath, String text) {
// TODO Auto-generated method stub
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getActivity().getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND_MULTIPLE);
targetedShare.setType("image/png"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
targetedShare.putExtra(Intent.EXTRA_TEXT, text);
ArrayList<Uri> files = new ArrayList<Uri>();
for(int j= 0;j<caminhos.size();j++){
if(!caminhos.get(j).isEmpty()){
File file = new File(caminhos.get(j));
Uri uri = Uri.fromFile(file);
files.add(uri);
}
}
targetedShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM,
files);
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
}
}
I want to display an activity chooser that shows all apps that can VIEW and/or EDIT some data. Is there an easy way to do this, or do I have to implement my own activity chooser dialog? Or maybe I can just subclass Intent? Thanks.
I found a partial solution by using EXTRA_INITIAL_INTENTS:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);
I say partial because if an app supports both ACTION_VIEW and ACTION_EDIT it will show up twice in the list, one of which will open the file for viewing and the other for editing, and you wouldn't necessarily know which is which. I think a complete solution would require a custom app chooser, as Tim suggested.
EDIT (Complete Solution!):
I found a solution that doesn't involving writing a custom app chooser. In order to differentiate ACTION_EDIT apps from ACTION_VIEW apps, I found a way to append a "(for editing)" string to the labels for one of them (in my case, ACTION_EDIT) by using the line of code Tim provided. In addition, to ensure the appended string doesn't appear to be a part of the app name, I changed the color of it to cyan:
PackageManager pm = kyoPrint.getPackageManager();
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent openInChooser = Intent.createChooser(viewIntent, "Open in...");
// Append " (for editing)" to applicable apps, otherwise they will show up twice identically
Spannable forEditing = new SpannableString(" (for editing)");
forEditing.setSpan(new ForegroundColorSpan(Color.CYAN), 0, forEditing.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
List<ResolveInfo> resInfo = pm.queryIntentActivities(editIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_EDIT);
intent.setDataAndType(uri, type);
CharSequence label = TextUtils.concat(ri.loadLabel(pm), forEditing);
extraIntents[i] = new LabeledIntent(intent, packageName, label, ri.icon);
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
EDIT 2: BUG
If there are no activities found by the first intent, NO activities will be displayed, including any found by the second intent. I ended up writing my own chooser. I just populated an ExpandableListView with headings for each type of intent with their respective activities as children (stored as individual LabeledIntents).
depends on what your data is. But in general using with ACTION_VIEW and some data attached you can use an IntentChoooser to populate the list of choices to the user.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some data");
startActivity(Intent.createChooser(intent, "Open with"));
Be sure to set your type correctly so that applications will know that you are wanting to open something that they may be able to handle.
EDIT: I think you would have to use a package manager query to get your two lists then combine them into one and make your own activity / dialog that will pop-up and get populated with the data contained in your combined list.
Here is an example making the query:
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
so if you make your two Intents and call this twice, passing in each intent you should be able to combine the resulting lists to get your full set of possibilities. Then it is up to to create an activity or dialog to show them with.