Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

Android: How to share image with text on facebook via intent?

I'd like to share a photo with caption pre-filled from my app via a share intent, on facebook.

Example code

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");      

intent.putExtra(Intent.EXTRA_TEXT, "eample");
intent.putExtra(Intent.EXTRA_TITLE, "example");
intent.putExtra(Intent.EXTRA_SUBJECT, "example");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);

Intent openInChooser = new Intent(intent);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);

Here is screen shot what I get

Text is not display

If a set type to image/* then a photo is uploaded without the text prefilled. If a set it to text/plain photo is not display.....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...