Android:如何通过意图在 facebook 上与文本共享图像? [英] Android: How to share image with text on facebook via intent?

查看:37
本文介绍了Android:如何通过意图在 facebook 上与文本共享图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过分享意图在 facebook 上分享一张从我的应用中预填了标题的照片.

I'd like to share a photo with caption pre-filled from my app via a share intent, on 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);

这是我得到的屏幕截图

如果将 type 设置为 image/* 则上传的照片没有预填充文本.如果将其设置为文本/普通照片不显示.....

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.....

推荐答案

最新的 Facebook 版本不允许您使用意图共享文本.你必须使用 Facebook SDK 来做到这一点——为了简单起见,使用 Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook).使用该库,您的代码将如下所示(摘自 Simple Facebook 站点):

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):

设置 OnPublishListener 并调用:

  • publish(Feed, OnPublishListener) 没有对话框.
  • publish(Feed, true, OnPublishListener) 带有对话框.
  • publish(Feed, OnPublishListener) without dialog.
  • publish(Feed, true, OnPublishListener) with dialog.
  • message - 用户的消息
  • name - 链接附件的名称
  • caption - 链接的标题(显示在链接名称下方)
  • description - 链接的描述(显示在链接标题下方)
  • picture - 附加到此帖子的图片的 URL.图片必须至少为 200 像素 x 200 像素
  • link - 附加到这篇文章的链接
  • 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

初始化回调监听器:

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)
      */
};

构建供稿:

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();

发布供稿没有对话框:

mSimpleFacebook.publish(feed, onPublishListener);

使用对话框发布供稿:

mSimpleFacebook.publish(feed, true, onPublishListener);



根据新的 Facebook SDK.


according to New Facebook SDK.

facebook-android-sdk:4.6.0

facebook-android-sdk:4.6.0

非常简单.
1.在 Android.manifest.xml 中创建 Provider

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.使用数据创建您的分享意图.

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.显示共享对话框

ShareDialog.show(ShowNavigationActivity.this,shareLinkContent);


就是这样.

这篇关于Android:如何通过意图在 facebook 上与文本共享图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆