Use Facebook sdk's ShareContent in share intent android - android

I am trying to use the shareintent in my app but I ran into a problem when sharing a link to facebook, no images is shown with the preview. So tried customizing the android shareintent so that it uses the share functionality from facebooksdk when facebook is selected but I can't seem to get it working. Below is the code that I tried for customizing the shareintent,
Intent share = new Intent(android.content.Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(share, 0);
for (final ResolveInfo app : activityList) {
if (app.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentTitle(property.PropertyName)
.setImageUrl(Uri.parse(property.ImagePath))
.setContentUrl(Uri.parse(property.PropertyPermaLink))
.build();
ShareDialog shareDialog = new ShareDialog(this);
shareDialog.canShow(content);
break;
} else {
share.setType("text/plain");
share.addFlags(share.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, "");
share.putExtra(Intent.EXTRA_TEXT, property.PropertyPermaLink);
startActivity(Intent.createChooser(share, "Share property!"));
}
}
After debugging the above code I found that the activitylist only consists of a single element. So how can I resolve this issue?

You need to add the Mime data type that the send intent is handling to get the appropriate activities returned:
share.setType("text/plain");

Just try SharePhotoContent instead of ShareLinkContent.
If you want using ShareLinkContent, please be sure that property.ImagePath pointed to real http image link. If link contain https check that is working in default android browser app.

Related

I can't share a text + link on facebook

Why I can't set a text when I'm sharing on facebook?
I have this code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String str = "My Text\n" + "http://play.google.com/store/apps/details?id=com.google.android.apps.maps";
shareIntent.putExtra(Intent.EXTRA_TEXT, str);
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().contains("com.facebook.katana")) {
shareIntent.setPackage(info.activityInfo.packageName);
startActivity(shareIntent);
break;
}
}
And it works, but only the link appears. Anyone Knows what is wrong?
(I don't want to use Facebook SDK.)
Thanks
Bad facebook! :-(
This is impossible, Because of you don't want use Facebook SDK.
You have to use the actual official separate Facebook Android SDK in your project to get the full sharing functionality. Which is extra work.
Change your decision, then see this:
https://developers.facebook.com/docs/sharing/android
ShareDialog shareDialog;
FacebookSdk.sdkInitialize(getApplicationContext());
shareDialog = new ShareDialog(UserProfileActivity.this);
ShareLinkContent content1 = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://Your url"))
.setContentTitle("name of content")
.setContentDescription("description")
.setImageUrl(Uri.parse("img url"))
.build();
shareDialog.show(content1);

Android share intent for Pinterest not working

I am doing an android share intent for Pinterest but is not fully working. I am able to attach the image but I can't send text to the "description" field in the share window. I've tried different types (text/plain, image/*, image/png) and also tried the ACTION_SEND_MULTIPLE intent type but still no luck. Google chrome share intent works perfectly so I'm sure Pinterest supports this functionality. Here is my code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, text);
if(file != null) intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setClassName(packageName, name);
this.startActivity(intent);
Any idea? thanks!
I found a way to share to Pinterest with plain Android intents (without using the Pinterest SDK), with help from Pin It button developer docs.
Basically you just open an URL like this with Intent.ACTION_VIEW; the official Pinterest app kindly supports these URLs. (I've earlier used a very similar approach for sharing to Twitter.)
https://www.pinterest.com/pin/create/button/
?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F
&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg
&description=Next%20stop%3A%20Pinterest
And for smoother user experience, set the intent to open directly in Pinterest app, if installed.
A complete example:
String shareUrl = "https://stackoverflow.com/questions/27388056/";
String mediaUrl = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
String description = "Pinterest sharing using Android intents"
String url = String.format(
"https://www.pinterest.com/pin/create/button/?url=%s&media=%s&description=%s",
urlEncode(shareUrl), urlEncode(mediaUrl), urlEncode(description));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
filterByPackageName(context, intent, "com.pinterest");
context.startActivity(intent);
Util methods used above:
public static void filterByPackageName(Context context, Intent intent, String prefix) {
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
intent.setPackage(info.activityInfo.packageName);
return;
}
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
}
catch (UnsupportedEncodingException e) {
Log.wtf("", "UTF-8 should always be supported", e);
return "";
}
}
This is the result on a Nexus 5 with Pinterest app installed:
And if Pinterest app is not installed, sharing works just fine via a browser too:
for some reason pinterest app doesn't comply to the standard (Intent.EXTRA_TEXT) so we have to add it separately
if(appInfo.activityInfo.packageName.contains("com.pinterest"){
shareIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION","your description");
}
File imageFileToShare = new File(orgimagefilePath);
Uri uri = Uri.fromFile(imageFileToShare);
Intent sharePintrestIntent = new Intent(Intent.ACTION_SEND);
sharePintrestIntent.setPackage("com.pinterest");
sharePintrestIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION", text);
sharePintrestIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharePintrestIntent.setType("image/*");
startActivityForResult(sharePintrestIntent, PINTEREST);

Sharing photo on facebook using intent Android?

i want to send photo using intent
i have try this code but my problem is if i use this code for sharing than it open all the applciation related to sharing i want to open only FACEBOOK AND TWITTER APP for sharing
Intent sharefacebook = new Intent(Intent.ACTION_SEND);
sharefacebook.setType("image/*");
sharefacebook.putExtra(Intent.EXTRA_TEXT, "From Android");
sharefacebook.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filepath.toString()));
startActivity(Intent.createChooser(sharefacebook, "Sharing"));
it open email,skype,gmail everthing i want to open only facebook and twitter it is possible
You can query the client apps(activities) and start the activity only if it's a fb/twitter client like this:
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo info : matches){
if (info.activityInfo.name.toLowerCase().contains("facebook") || info.activityInfo.name.toLowerCase().contains("twitter")){
Intent sharefacebook = new Intent(Intent.ACTION_SEND);
sharefacebook.setType("image/*");
sharefacebook.putExtra(Intent.EXTRA_TEXT, "From Android");
sharefacebook.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filepath.toString()));
startActivity(Intent.createChooser(sharefacebook, "Sharing"));
}else{
Toast.makeText(getApplicationContext(), "FB or twitter client not installed", Toast.LENGTH_SHORT).show();
}
}

Android image share intent - how can I exclude a particular handler from the list?

In my Android App I have an image loaded from Instragram API and I share it via the generic Share Intent like this:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(share, "Share Image"));
By default it gives a list of all installed apps that handle this intent, including Instagram which is the original source from where the image is coming from. How can I filter this list to exclude only the Instagram App from handling the intent while leaving all the rest?
How can I filter this list to exclude only the Instagram App from handling the intent while leaving all the rest?
You can't, except by creating your own chooser activity, using queryIntentActivities() on PackageManager and somehow filtering out Instagram. Doing that will be unreliable, simply because there are multiple possible Instagram clients, whose package names are not conclusively knowable in advance.
I'd just leave Instagram there as an option.
I used this code to filter the shareintent for instagram.
List<Intent> targets = new ArrayList<Intent>();
Intent template = new Intent(Intent.ACTION_SEND);
template.setType("text/plain");
List<ResolveInfo> candidates = this.getPackageManager().
queryIntentActivities(template, 0);
// remove all intent except instagram in share intent
for (ResolveInfo candidate : candidates) {
String packageName = candidate.activityInfo.packageName;
if (packageName.contains("instagram")) {
Intent target = new Intent(android.content.Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Text to share"));
target.setPackage(packageName);
targets.add(target);
}
}
Intent chooser = Intent.createChooser(targets.remove(0), translate("Share Via"));
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targets.toArray(new Parcelable[]{}));
startActivity(chooser);

Android Share intent - facebook

i want to share something with my friends. so i preferred android share intent.
i used,
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_TEXT, "my share text");
startActivity(Intent.createChooser(i, "share via"));
this showing a list of available apps like Facebook, Twitter, Gmail, Message, Skype.. e.t.c..
If i pressed a Twitter in the sense, the above text "my share text" showing in the tweet text box.
But if i select Facebook, the status message not showing.
i want to set the status message programatically.
how can i achieve this?
Facebook SDK has this bug, it's pretty annoying, I know. But if you set a link (and only a link) as "my share text", it will appear in the facebook share box.
I just built this code and it's working for me:
private void shareAppLinkViaFacebook() {
String urlToShare = "YOUR_URL";
try {
Intent intent1 = new Intent();
intent1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
intent1.setAction("android.intent.action.SEND");
intent1.setType("text/plain");
intent1.putExtra("android.intent.extra.TEXT", urlToShare);
startActivity(intent1);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
}
Facebook does not allow the pre populating of text in the status box like as twitter. But we can pass the text with url to the Facebook. And it appears below the status box. check below code.
NOTE : Use Facebook SDK to share [ Recommended ].
Native share through intent
Intent shareIntent= new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");
shareIntent.setType("text/plain");
shareIntent.setPackage("com.facebook.katana");
startActivity(shareIntent);
Share through Facebook SDK
ShareDialog shareDialog;
// Sharing in Facebook using the SDK
FacebookSdk.sdkInitialize(this);
shareDialog = new ShareDialog(this);
String title = "Demo Title";
String URL = "http://www.google.com";
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle(title).setContentDescription(URL)
.setContentUrl(Uri.parse(URL)).build();
shareDialog.show(linkContent);
Check the link below for reference.
Link 1
Let me know for queries or clarification.

Categories

Resources