Sharing photo on facebook using intent Android? - 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();
}
}

Related

intent share facebook - Android

I want to share an image from android activity to facebook messenger app which is already opened in my device and reside in recent tasks of the android device.
I am using the below code to share the image but it is opening a new activity in messenger 'share seperately' but I want to share it to the already opened chat.
String mimeType = "image/*";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setPackage("com.facebook.orca");
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
intent.putExtra(EXTRA_PROTOCOL_VERSION, PROTOCOL_VERSION);
intent.putExtra(EXTRA_APP_ID, YOUR_APP_ID);
activity.startActivityForResult(shareIntent, SHARE_TO_MESSENGER_REQUEST_CODE);
Facebook has restricted the text sharing over the post. It will either support a valid URL link sharing or Image Sharing. So I've posted the code which is referenced from https://stackoverflow.com/a/22994833/8331006
private void shareAppLinkViaFacebook(String urlToShare) {
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
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
}

Use Facebook sdk's ShareContent in share intent 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.

Share via Twitter Native App

I have android app where i am trying to add share feature via twitter.i have successfully implemented where the user can share via Twitter app :
This is my code:
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setType("text/plain");
tweetIntent.putExtra(Intent.EXTRA_SUBJECT, "tweet");
tweetIntent.putExtra(Intent.EXTRA_TEXT, "sample tweet");
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo ri : lract) {
if (ri.activityInfo.name.contains("twitter")) {
tweetIntent.setClassName(ri.activityInfo.packageName,
ri.activityInfo.name);
resolved = true;
break;
}
}
if(resolved){
activity.startActivity(tweetIntent);
}else{
// do something
}
Scenarios on click of button:
1) opens the twitter native app and if the user has already logged in it pass the control to post tweet page where my text will be added and on click of post it will successfully post.
ISSUE scenario
If the user has not logged in, user will sign in, after that it will move to Home tab where all the tweets are displayed, instead it should have moved to post tweet page where my text will be added.
how can i fix this issue ?
As I remember this should work with native twitter
String twitterPackage = "com.twitter.android";
String errorMessage = "You should install Twitter app first";
if(isPackageInstalled(twitterPackage, getActivity())){
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setType("text/*");
tweetIntent.setPackage(twitterPackage);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "sample tweet");
getActivity().startActivity(tweetIntent);
} else {
Toast.makeText(getActivity(), errorMessage, Toast.LENGHT_SHORT).show();// handle error
}

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.

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