在Android上与Facebook共享图像和文本

在Android上与Facebook共享图像和文本,android,facebook,android-intent,sharing,Android,Facebook,Android Intent,Sharing,Facebook,为什么你不在共享意图中拍摄图片和文本 我正在尝试使用标准的Android共享意图来共享图像和一些文本。我的分享意向是正确的,我在那里有形象,我已经完成了我的交易。我的代码,让我向您展示: public void doShare() { File image = getShareFile(); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND);

Facebook,为什么你不在共享意图中拍摄图片和文本

我正在尝试使用标准的Android共享意图来共享图像和一些文本。我的分享意向是正确的,我在那里有形象,我已经完成了我的交易。我的代码,让我向您展示:

public void doShare() {
    File image = getShareFile();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("*/*");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, resultObject.getShareSubject());
    shareIntent.putExtra(Intent.EXTRA_TEXT, resultObject.getShareText());
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
    shareActionProvider.setShareIntent(shareIntent);
}
Facebook出现在处理我意图的应用列表中,但它是谎言!!!当点击Facebook进行实际分享时,它会说:

请仅附上照片或单个视频

为什么是Facebook,为什么?为什么你会显示为一个可以处理我意图的应用程序,而不处理我的意图却让我的用户觉得我很愚蠢?你答应过Facebook


我在这个网站和网络上看到了很多关于这个的帖子。有没有人在不使用API的情况下实现了这一功能?

为什么?因为他们是Facebook,可以允许自己使用这些bug。 因此,他们只希望在类型为“image/”时使用图像。如果类型为“text/plain”-则消息中应包含URL。不要使用.setType(“/*”);
否则,请使用他们的SDK。但在这种情况下,您将牺牲应用程序的简单性和灵活性。

只有一种解决方案可以使用画布创建文本和图像的位图,并在facebook上共享。()

File image = getShareFile(); // name of files

Intent shareIntent = new Intent();

shareIntent.setAction(Intent.ACTION_SEND);

shareIntent.setType("*/*");

shareIntent.putExtra(Intent.EXTRA_SUBJECT, resultObject.getShareSubject());

shareIntent.putExtra(Intent.EXTRA_TEXT, resultObject.getShareText()); // sharing text 

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image)); // sharing image 

shareActionProvider.setShareIntent(shareIntent);
这是我的代码:

主要活动

 <RelativeLayout android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android">

<EditText
    android:id="@+id/et_text"
    android:layout_width="match_parent"
    android:textSize="15dp"
    android:layout_height="45dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/edittext_drawable"
    android:hint="Enter your text"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:paddingRight="10dp"
    android:inputType="text"
    android:imeOptions="actionDone"
    android:paddingLeft="10dp"
    android:singleLine="true"
    android:textColorHint="#979797" />


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl_main"
    android:background="#ffffff"
    android:layout_below="@+id/et_text"
    android:layout_above="@+id/tv_share">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:src="@drawable/index"
        android:scaleType="fitXY"
        android:id="@+id/iv_image"
        android:layout_marginTop="10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:id="@+id/tv_text"
        android:layout_below="@+id/iv_image"
        android:layout_margin="10dp"
        android:textColor="#000000"
        android:maxLines="5"
        />

</RelativeLayout>



<TextView
    android:id="@+id/tv_share"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#F38D0A"
    android:gravity="center"
    android:padding="10dp"
    android:layout_margin="10dp"
    android:text="Share"
    android:textColor="#ffffff"
    android:textSize="15dp"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

以下突出问题的解决方案

请仅附上照片或单个视频。

Facebook不在股票期权对话框中

即使出现Facebook墙,它也不会让你在头版打开你的图像。

不使用Facebook SDK在Facebook上共享图像

经过三天的不断寻找,我终于走运了。很抱歉,答案可能会很长,因为我在StackOverflow上找不到任何解决方案可以不使用Facebook SDK在Facebook上共享图像。

以下是完整而完美的解决方案:

Bitmap bitmap = viewToBitmap(load your image OR layout background for your imageView);
shareImage(bitmap, "com.facebook.katana");
第一个参数是加载到位图中的图像的imageView/布局。第二个参数是facebook的包名

然后,如图所示对以下方法进行编码

public void shareImage(Bitmap bitmap, String packageName) {
        try {
            File file = new File(this.getExternalCacheDir(), "temp.png");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 95, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            file.setReadable(true, false);
            final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/*");
            intent.setPackage(packageName);
            startActivity(Intent.createChooser(intent, "Share image on facebook"));
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            Toast.makeText(this, "Error while sharing Image on Facebook", Toast.LENGTH_SHORT).show();
        }
    }
以下是我的应用程序截图:

。“报价书应用程序”正在加载带有报价的图片

。单击共享按钮,然后从对话框中单击Facebook

。Facebook共享选项对话框出现

。图像加载到Facebook首页

。最后,该图像在Facebook上共享,并且该方法如我们预期的那样正常工作

PS:你可以在我的应用程序中查看这个功能,从google play商店下载

public void shareImage(Bitmap bitmap, String packageName) {
        try {
            File file = new File(this.getExternalCacheDir(), "temp.png");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 95, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            file.setReadable(true, false);
            final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/*");
            intent.setPackage(packageName);
            startActivity(Intent.createChooser(intent, "Share image on facebook"));
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            Toast.makeText(this, "Error while sharing Image on Facebook", Toast.LENGTH_SHORT).show();
        }
    }