我想在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);
这是我获得的屏幕截图
如果设置类型为图像/ *,则上传照片时不会预填充文本。如果设置为文本/普通照片不显示.....
答案 0 :(得分:44)
最新的Facebook版本不允许您使用意图共享文本。你必须使用Facebook SDK才能做到这一点 - 使用Facebook SDK + Android Simple Facebook(https://github.com/sromku/android-simple-facebook)。使用该库,您的代码将是这样的(从简单Facebook网站中提取):
设置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)
*/
};
构建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();
发布Feed ,无对话框:
mSimpleFacebook.publish(feed, onPublishListener);
使用对话框发布Feed :
mSimpleFacebook.publish(feed, true, onPublishListener);
根据新的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" />
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);
那就是它。
答案 1 :(得分: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")); }
答案 2 :(得分:3)
截至2017年,facebook不允许直接在您的应用中共享图片+文字。
作为一种解决方法,您可以创建单个页面应用程序*,动态加载您要共享的文本/图像(在URL中指定),然后您可以在Facebook上共享该URL。
注意:
0)将facebook to create an image preview添加到build.gradle文件
compile group: 'com.facebook.android', name: 'facebook-android-sdk', version: '4.25.0'
1)在AndroidManifest.xml中,在<application>
部分添加元数据标记:
<application android:label="@string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
</application>
将facebook_app_id字符串(带有您的APP ID)添加到strings.xml文件中:
<string name="facebook_app_id">12341234</string>
YOURFBAPPID是您在latest facebook-sdk library
找到的Facebook应用ID号 2)还在AndroidManifest.xml中的<provider>
标记之外添加<application>
标记
<provider android:authorities="com.facebook.app.FacebookContentProviderYOURFBAPPID"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
3)使用其构建器创建ShareLinkContent对象:
ShareLinkContent fbShare = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("http://yourdomain.com/your-title-here/someimagefilename"))
.build();
4)从你的片段(或活动等)中分享它:
ShareDialog.show(getActivity(), fbShare);
答案 3 :(得分:2)
FB不再允许您预先填写共享消息。
为了避免这种情况,您需要使用SDK通过Graph请求进行发布。为此,您需要publish_actions
权限。自上个月开始,您需要submit your app to a review process才能访问publish_actions
。如果您的应用预先填写共享文本,您将失败。相信我 - 我有Chutzppah尝试。
所以看起来我们必须遵守。
B.t.w。在iOS中,您仍然可以使用FB sdk预填充文本。谁知道多久。
答案 4 :(得分:0)
如果不使用Facebook sdk,我们无法在Facebook上同时共享图片和文字。为了解决这个问题,我创建了一个图像和文本的位图,在Facebook上分享该位图,它的工作正常。
您可以从此处下载源代码(Share image and text on facebook using intent in android)
这是代码:
<强> MainActivity.java 强>
package com.shareimage;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
EditText et_text;
ImageView iv_image;
TextView tv_share,tv_text;
RelativeLayout rl_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
et_text = (EditText)findViewById(R.id.et_text);
iv_image = (ImageView)findViewById(R.id.iv_image);
tv_share = (TextView)findViewById(R.id.tv_share);
rl_main = (RelativeLayout)findViewById(R.id.rl_main);
tv_text= (TextView) findViewById(R.id.tv_text);
File dir = new File("/sdcard/Testing/");
try {
if (dir.mkdir()) {
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}
} catch (Exception e) {
e.printStackTrace();
}
tv_share.setOnClickListener(this);
et_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
tv_text.setText(et_text.getText().toString());
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_share:
Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
saveBitmap(bitmap1);
String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";
fn_share(str_screenshot);
break;
}
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Log.e("ImageSave", "Saveimage");
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
public void fn_share(String path) {
File file = new File("/mnt/" + path);
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Image"));
}
}
答案 5 :(得分:0)
在此公式中,您可以共享Messenger和Instagram(com.instagram.android)的图像,而无需在“ AndroidManifest
中使用任何提供程序public void shareMessenger(View v) {
// showToast("checking");
File dir = new File(Environment.getExternalStorageDirectory(), "MyFolder");
File imgFile = new File(dir, "Image.png");
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("image/*");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imgFile));
sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
sendIntent.setPackage("com.facebook.orca");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(Intent.createChooser(sendIntent, "Share images..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(SaveAndShareActivity.this, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
}
}
**在onCreate方法中添加这两行**
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
答案 6 :(得分:-1)
将这些行添加到以下代码
shareCaptionIntent.putExtra(Intent.EXTRA_TITLE, "my awesome caption in the EXTRA_TITLE field");
答案 7 :(得分:-3)
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}