Android:如何通过意图在Facebook上与文本共享图像?

Man*_*nsi 45 android facebook android-intent

我想通过Facebook上的分享意图与我的应用程序预先填充的标题分享照片.

示例代码

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);
Run Code Online (Sandbox Code Playgroud)

这是我得到的屏幕截图

文本未显示

如果设置类型为图像/*,则上传照片时不会预填充文本.如果设置为文本/普通照片不显示.....

小智 45

最新的Facebook版本不允许您使用意图共享文本.你必须使用Facebook SDK才能做到这一点 - 使用Facebook SDK + Android Simple Facebook(https://github.com/sromku/android-simple-facebook).使用该库,您的代码将是这样的(从Simple Facebook站点中提取):

发布Feed

设置OnPublishListener并致电:

  • publish(Feed, OnPublishListener) 没有对话.
  • publish(Feed, true, OnPublishListener) 用对话框.

基本属性

  • message - 用户的消息
  • name - 链接附件的名称
  • caption - 链接标题(显示在链接名称下方)
  • description - 链接描述(显示在链接标题下方)
  • picture - 此帖子附带的图片的网址.图片必须至少为200px×200px
  • link - 此帖子附带的链接

初始化回调侦听器:

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)
      */
};
Run Code Online (Sandbox Code Playgroud)

建立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();
Run Code Online (Sandbox Code Playgroud)

不带对话框发布Feed :

mSimpleFacebook.publish(feed, onPublishListener);
Run Code Online (Sandbox Code Playgroud)

使用对话框发布Feed :

mSimpleFacebook.publish(feed, true, onPublishListener);
Run Code Online (Sandbox Code Playgroud)


2015年12月14日更新


根据新的Facebook SDK.

Facebook的Android的SDK:4.6.0

这很简单.
1.创建提供者Android.manifest.xml

<provider
            android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />
Run Code Online (Sandbox Code Playgroud)

2.使用数据创建您的共享意图.

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();
Run Code Online (Sandbox Code Playgroud)


3.显示"共享"对话框

ShareDialog.show(ShowNavigationActivity.this,shareLinkContent);
Run Code Online (Sandbox Code Playgroud)


而已.

  • 我认为Facebook*SDK*需要一个"简单的facebook"库具有讽刺意味.我无法克服它,我要大声掏出我的内心. (9认同)

Ric*_*ard 13

试试这个

private void initShareIntent(String type,String _text){
     File filePath = getFileStreamPath("shareimage.jpg");  //optional //internal storage
     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
     shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));  //optional//use this when you want to send an image
     shareIntent.setType("image/jpeg");
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     startActivity(Intent.createChooser(shareIntent, "send"));
}

  • 截至目前,这只会将图像共享到Facebook。 (2认同)
  • 它唯一的图像,但仍然很受赞赏(Y) (2认同)
  • 如果我也想输入文字,该怎么办? (2认同)