繁体   English   中英

在 Android 上与 Facebook 分享图片和文字

[英]Sharing images and text with Facebook on Android

Facebook,为什么share Intent不拍图和文字?

在此处输入图像描述

我正在尝试使用标准 Android 共享 Intent 来共享图像和一些文本。 我的分享 Intent 设置正确,图像已放入其中,我已经完成了交易。 我的代码,让我给你看:

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 出现在将处理我的 Intent 的应用程序列表中,但它位于:!! 当点击 Facebook 进行实际分享时,它说:

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

为什么是Facebook,为什么? 为什么你显示为可以处理我的 Intent 的应用程序,然后通过不处理 Intent 让我的用户看起来很愚蠢? 你答应了Facebook。

我在整个网站和 web 上看到了很多关于此的话题。有没有人不用他们的 API 就可以让它工作?

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

这是我的工作解决方案如何通过意图在Facebook上分享图像。 不幸的是,Facebook不接受EXTRA_TEXT所以它只能共享图像。 它也可以与其他应用程序一起使用,并且大多数应用程序也可以添加文本标题。

    /**
     * Show share dialog
     * @param file image to share
     * @param text text to add for sharing
     */
    private void shareImageAndTextResultIntent(File file, String text) {
        // share intent
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        shareIntent.setType("image/*");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, "send"));
    }

如果您想与图片共享图像,则必须使用Facebook SDK。

我不知道是否真的有意与Facebook分享,但在这里,请查看Twitter的这个链接。 这篇文章以及答案似乎是一篇精心编写的详细文章。

Android Intent for Twitter应用程序

只有一种解决方案,你可以使用画布创建文本和图像的位图,并在Facebook上分享。 在这里下载源代码

这是我的代码:

activity_main

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

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


}
}

解决以下突出显示的问题。

Please attach only photos or a single video.

Facebook isn't in the Share option Dialog

even if Facebook wall appears, then it won't let you open your image on the front page.

Share an image on Facebook without using Facebook SDK

我经常搜索三天后很幸运。 对不起,答案可能很长,因为我发现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();
        }
    }

这是我的应用截图:

1 “引用书App”正在加载带有引号的图片。

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

3 出现Facebook共享选项对话框。

4 图片加载到Facebook首页。

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

PS:你可以在我的应用程序中查看此功能, 这是从谷歌游戏商店下载它的链接

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM