Android: How to share image with text on facebook via intent?
The newest Facebook versions doesn't allow you to share text using intents. You have to use the Facebook SDK to do it - to make that simple, use the Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook). Using the library, your code would be like this (extracted from the Simple Facebook site):
Publish feed
Set OnPublishListener
and call for:
-
publish(Feed, OnPublishListener)
without dialog. -
publish(Feed, true, OnPublishListener)
with dialog.
Basic properties
-
message
- The message of the user -
name
- The name of the link attachment -
caption
- The caption of the link (appears beneath the link name) -
description
- The description of the link (appears beneath the link caption) -
picture
- The URL of a picture attached to this post. The picture must be at least 200px by 200px -
link
- The link attached to this post
Initialize callback listener:
OnPublishListener onPublishListener = new OnPublishListener() {
@Override
public void onComplete(String postId) {
Log.i(TAG, "Published successfully. The new post id = " + postId);
}
/*
* You can override other methods here:
* onThinking(), onFail(String reason), onException(Throwable throwable)
*/
};
Build feed:
Feed feed = new Feed.Builder()
.setMessage("Clone it out...")
.setName("Simple Facebook for Android")
.setCaption("Code less, do the same.")
.setDescription("The Simple Facebook library project makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.")
.setPicture("https://raw.github.com/sromku/android-simple-facebook/master/Refs/android_facebook_sdk_logo.png")
.setLink("https://github.com/sromku/android-simple-facebook")
.build();
Publish feed without dialog:
mSimpleFacebook.publish(feed, onPublishListener);
Publish feed with dialog:
mSimpleFacebook.publish(feed, true, onPublishListener);
Update on 14 December 2015
according to New Facebook SDK.
facebook-android-sdk:4.6.0
It's very Simple.
1. create Provider in Android.manifest.xml
<provider
android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
2. Create Your Share Intent with Data.
ShareHashtag shareHashTag = new ShareHashtag.Builder().setHashtag("#YOUR_HASHTAG").build();
ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
.setShareHashtag(shareHashTag)
.setQuote("Your Description")
.setContentUrl(Uri.parse("image or logo [if playstore or app store url then no need of this image url]"))
.build();
3. Show The Share Dialog
ShareDialog.show(ShowNavigationActivity.this,shareLinkContent);
That's It.
As of 2017, facebook doesn't allow sharing of an image + text together, directly from your app.
Workaround
Facebook will though, scrape a URL for title and image data and will use that in a share post.
As a workaround you could create a single page application* that dynamically loads the text/image you want to share (specified in the URL) and you can facebook-share that URL.
Notes:
- Ensure your single page application produces a static page which has its title, open graph meta tags, and images set prior to facebook's page scrape. If these web page tags are changed dynamically through Javascript, facebook will not be able to scrape those values and use them in its share post.
- Use open graph meta property tags og:image:height and og:image:width to allow facebook to create an image preview within its share post
Steps
0) add the latest facebook-sdk library to your build.gradle file
compile group: 'com.facebook.android', name: 'facebook-android-sdk', version: '4.25.0'
1) In your AndroidManifest.xml, add a meta-data tag within your <application>
section:
<application android:label="@string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
</application>
Add a facebook_app_id string (with your APP ID) to your strings.xml file:
<string name="facebook_app_id">12341234</string>
YOURFBAPPID is your Facebook App ID number found at https://developers.facebook.com/apps/
2) also add a <provider>
tag outside of your <application>
tag in AndroidManifest.xml
<provider android:authorities="com.facebook.app.FacebookContentProviderYOURFBAPPID"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
3) Create a ShareLinkContent object using their builder:
ShareLinkContent fbShare = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("http://yourdomain.com/your-title-here/someimagefilename"))
.build();
4) Share it from your fragment (or activity, etc.):
ShareDialog.show(getActivity(), fbShare);
Facebook Docs
https://developers.facebook.com/docs/android/getting-started