Android:如何通过意图与Facebook上的文字分享图像?

我想分享一个照片与预先从我的应用程序通过共享意图,在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); 

这里是我得到的屏幕截图

文字不显示

如果设置的types为图片/ *,则上传照片而不预先填写文本。 如果设置为文本/纯文本照片不显示…..

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

发布Feed

设置OnPublishListener并呼吁:

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

基本属性

  • message – 用户的消息
  • name – 链接附件的名称
  • caption – 链接的标题(出现在链接名称下方)
  • description – 链接的描述(出现在链接标题下)
  • picture – 附加到此post的图片的url。 图片必须至less200px x 200px
  • link – 附加到这个职位的链接

初始化callback监听器:

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

发布没有对话的Feed:

 mSimpleFacebook.publish(feed, onPublishListener); 

使用对话框发布Feed:

 mSimpleFacebook.publish(feed, true, onPublishListener); 

2015年12月14日更新

根据新的Facebook SDK。

Facebook的Android的SDK:4.6.0

这很简单。
1.在Android.manifest.xml创buildProvider

 <provider android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}" android:name="com.facebook.FacebookContentProvider" android:exported="true" /> 

2.用数据创build你的共享意图。

 ShareLinkContent shareLinkContent = new ShareLinkContent.Builder() .setContentTitle("Your Title") .setContentDescription("Your Description") .setContentUrl(Uri.parse("URL[will open website or app]")) .setImageUrl(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); 

而已。

试试这个

 private void initShareIntent(String type,String _text){
      File filePath = getFileStreamPath(“shareimage.jpg”);  //可选//内部存储
      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)));  //可选//当你想发送图像时使用这个
      shareIntent.setType( “图像/ JPEG”);
      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      startActivity(Intent.createChooser(shareIntent,“send”));
 }

截至2017年,Facebook不允许直接从您的应用程序共享图像+文本。

解决方法

Facebook将尽pipe,刮了标题和图像数据的URL,并将在一个共享post中使用。

作为一种解决方法,您可以创build一个单独的页面应用程序*,它dynamic加载您要共享的文本/图像(在URL中指定),并且您可以通过Facebook共享该URL。

笔记:

  • 确保您的单页应用程序生成一个静态页面,其标题,打开的graphics元标记,以及在Facebook页面刮除之前设置的图像。 如果这些网页标签通过Javascriptdynamic更改,Facebook将无法刮取这些值并将其用于共享post中。
  • 使用打开的graphics元属性标签og:image:height和og:image:width允许Facebook在其分享信息中创build图像预览

脚步

0)将最新的facebook-sdk库添加到您的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_idstring(带有你的APP ID)到你的strings.xml文件中:

 <string name="facebook_app_id">12341234</string> 

YOURFBAPPID是您的Facebook应用程序ID号码,位于https://developers.facebook.com/apps/

2)还要在AndroidManifest.xml的<application>标签之外添加一个<provider>标签

 <provider android:authorities="com.facebook.app.FacebookContentProviderYOURFBAPPID" android:name="com.facebook.FacebookContentProvider" android:exported="true"/> 

3)使用其构build器创build一个ShareLinkContent对象:

 ShareLinkContent fbShare = new ShareLinkContent.Builder() .setContentUrl(Uri.parse("http://yourdomain.com/your-title-here/someimagefilename")) .build(); 

4)从你的片段(或活动等)分享:

 ShareDialog.show(getActivity(), fbShare); 

Facebook文件

https://developers.facebook.com/docs/android/getting-started

FB不再允许您预先填写共享消息。

为了规避这一点,您将需要使用SDK通过Graph请求进行发布。 为此,您将需要publish_actions权限。 自上个月以来,您需要将您的应用提交到审核stream程才能访问publish_actions 。 如果您的应用程序预先填充共享文本,那么您将会失败。 相信我 – 我有Chutzppah试试。

所以看起来我们必须遵守。

Btw在iOS中,你仍然可以预先使用FB sdk文本。 谁知道多久。

如果不使用Facebook SDK,我们不能在Facebook上同时分享图片和文字。 为了解决这个问题,我创build了一个图片和文字的位图,在Facebook上分享这个位图,并且完美地工作。

你可以从这里下载源代码( 在Android上使用意图在Facebook上分享图像和文字 )

这是代码:

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")); } } 

将这些行添加到您的下面的代码

 shareCaptionIntent.putExtra(Intent.EXTRA_TITLE, "my awesome caption in the EXTRA_TITLE field"); 
 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; } }