我想通过分享 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);
这是我得到的屏幕截图
如果将类型设置为 image/*,则上传照片时不预填文本。如果将其设置为文本/普通照片不显示.....
最佳答案
最新的 Facebook 版本不允许您使用 Intent 共享文本。您必须使用 Facebook SDK 来做到这一点 - 为了简单起见,请使用 Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook)。使用该库,您的代码将如下所示(摘自 Simple Facebook 站点):
发布供稿
设置OnPublishListener
并调用:
publish(Feed, OnPublishListener)
不带对话框。publish(Feed, true, OnPublishListener)
带对话框。
基本属性
message
- 用户的消息name
- 链接附件的名称caption
- 链接的标题(显示在链接名称下方)description
- 链接的描述(出现在链接标题下方)picture
- 此帖子所附图片的 URL。图片必须至少为 200 x 200 像素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)
*/
};
构建提要:
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);
2015 年 12 月 14 日更新
根据新的 Facebook SDK。
facebook-android-sdk:4.6.0
这很简单。
1。在 Android.manifest.xml
中创建 Provider
<provider
android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
2.使用数据创建您的共享 Intent 。
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:如何通过 Intent 在 Facebook 上共享图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533773/