Android - Using dynamic text field for sharing using chooser dialog - android

If the user chooses Twitter, I want to send an abbreviated text field (due to character limit). From this SO post -- Branching the Android Share Intent extras depending on which method they choose to share -- I learned that I can implement targeted intents. However, when I use the code below, a number of apps show as "Android System" and Twitter does not show up in the dialog. I even removed the if block trying to catch Twitter, and it still did not appear.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
PackageManager pm = rootView.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList){
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
if(TextUtils.equals(packageName, TWITTER_PACKAGE_NAME)){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I'm not really sure what's going on with the following:
Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
In the SO post, targetedShareIntents.remove(0) was used although targetedShareIntents.remove(targetedShareIntents.size() - 1) was proposed as a more reusable solution. Any help or explanations on this is greatly appreciated.

I think TextUtils.equals(packageName, TWITTER_PACKAGE_NAME) can't find exactly com.twitter.android package name, so you need to try below code for match Twitter package name
if(packageName.toLowerCase().startsWith("com.twitter")){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}

Related

Is it possible to exclude apps and directshare?

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");

Creating custom chooser for share intent by package name

I have a Text in my android activity and I want give the user option to share it on social apps like whatsapp, line, facebook, twitter etc.
But I want to create a custom chooser so that it won't show unintended apps in the chooser.
I'm aware of this snippet
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "Share via"));
How can I make it so that in the chooser it'd only show the apps which I can specify by their package names.
Thanks
Even though I think this question is duplicated, but since I can't find the duplicated question yet, let me provide an answer first.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareInent = new Intent(Intent.ACTION_SEND);
shareInent.setType("text/plain");
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(shareInent, 0);
// put the name of the packages you want in this ArrayList
ArrayList<String> wantedPackage = new ArrayList<>();
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("text/plain");
String infoPackageName = info.activityInfo.packageName.toLowerCase();
if (wantedPackage.contains(infoPackageName)) {
targetedShare.putExtra(Intent.EXTRA_TEXT, "put your text here");
targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
targetedShareIntents.add(targetedShare);
resPackageNames.add(infoPackageName);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Chooser title");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}

Share text/image on Facebook

I am new on progamming android and I have some problems on sharing contents.
I did an android app that involves a test. In the last activity I'd like to offer the opportunity to share the users results via shareintent, here a frame of the code:
display= (TextView) findViewById (R.id.tvDisplay);
display.setText("Well Done, your score is" + score ); //score is an int
sharebutton= (Button)findViewById(R.id.sharebutton);
sharebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
results = display.getText().toString();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,"My score in the test is" + results);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TEST");
startActivity(Intent.createChooser(shareIntent, "Share.."));
}
});
The problem is that when I click on the button, I can share the exact text in all of sharer tools (email, wapp, etc..), but not through Facebook. Why?
I tried to allow to post image and text on the FB wall but I can't give the exact path of the image that is on the drawable Folder.
Thank you for all the advices.
It depends upon Facebook app whether they are accepting text or not via intent,I know they accept images and bitmap files but for text they don't accept it.I've already tried this earlier in one of my own project but couldn't supply text via intent.
The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.
I have already answer this question here.May be you can follow this link in case you need reference.
first store image in sdcard then share image on facebook
File filePath = new File(Environment
.getExternalStorageDirectory(),
"/foldername/imagename.jpg");
// share("facebook", filePath.toString());
System.out.println(filePath);
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
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);
targetedShare.setType("image/jpeg"); // put here your
// mime
// type
if (info.activityInfo.packageName.toLowerCase()
.contains("facebook")
|| info.activityInfo.name.toLowerCase()
.contains("facebook")) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,
"This photo is created by App Name");
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(filePath));
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);
}

Share image and text using intent not work in android?

i want to share image and text using intent but it is not work
when facebook intent start it display only image i want to share both text and image
you can check the image here
List<Intent> targetShareIntents=new ArrayList<Intent>();
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
if(!resInfos.isEmpty())
{
for(ResolveInfo resInfo : resInfos)
{
String packageName=resInfo.activityInfo.packageName;
Log.i("Package Name", packageName);
if(packageName.contains("com.facebook.katana"))
{
String imageurl="/storage/emulated/0/iWally/explosion_3-wallpaper-1024x768.jpg";
String text="hello how are you";
Intent intent=new Intent();
intent.setType("image/*");
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,text);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+imageurl.toString()));
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if(!targetShareIntents.isEmpty())
{
System.out.println("Have Intent");
Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
else
{
Uri uri = Uri.parse("market://details?id=com.facebook.katana");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
System.out.println("Do not Have facebook");
}
}
Hey I did a lot of research into this as I asked the same question
Basically it is not possible from the facebook app using "com.facebook.katana" as it ignores the extra text when and image is there see this for actual bug but can have links when the image is not there. Very annoying I know.
After a lot of looking about I created my own activity using the facebook sdk 3.14.1 which allows images and text here is the github to the demo project give it a go and let know if it helps you out.

Branching the Android Share Intent extras depending on which method they choose to share

Simply want to share a dynamic text string + the URL to the app. The native Android share intent is setup correctly, and works perfect with Twitter, Gmail, etc. But, as many will guess, it does not work with Facebook. Appearantly because Facebook will not accept text in the intent.EXTRA_TEXT field, only a single URL.
Well, my question to y'all is: is there a way to branch off the share intent extras depending on which method they choose to share? for example, if they share via gmail or Twitter, use the existing String + URL (the desired option) EXTRA_TEXT, but if they choose to share via Facebook, only use a URL as the EXTRA_TEXT.
Not really wanting to implement the Facebook Android SDK for such a simple task that is built-in natively in Android.
Appreciate your time and advice.
Tried something like this, but it obviously fails because its only checking if the sharing option exists (when share pops up, not after they click a share method), it doesn't respond when they choose a method.
String shareBody = "app string text " + act_txt + " more text! Get the app at http://www.appurl.com";
PackageManager pm = view.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList) {
Log.i(TAG, "app.actinfo.name: " + app.activityInfo.name);
//if((app.activityInfo.name).contains("facebook")) {
if("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) {
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.appurl.com");
startActivity(Intent.createChooser(sharingIntent, "Share idea"));
break;
} else {
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "app name");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share"));
break;
}
}
found a solution, on this SO question asking for something else: https://stackoverflow.com/a/8550043/1938669
the attempt posted my original question here was close. within that cycle of possible shareIntent List, you need to create a new share intent targeted at the specific sharing choice (like facebook or twitter)
here is a final working solution that shares only a URL if facebook is choosen, otherwise shares the complete text string + url:
public void shareIt(View view){
//sharing implementation
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";
PackageManager pm = view.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList) {
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
if(TextUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
} else {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}

Categories

Resources