2

我正在使用下面的代码在 Android 中共享图像和文本。当我选择 Whatsapp 时,它会同时分享图片和文字,但当我选择 Facebook 时,它只会分享图片而没有任何文字!我的代码有什么错误?谢谢。

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(bitmapPath)));
share.putExtra(Intent.EXTRA_TEXT, "Shared via" +  APP_URL);
startActivity(Intent.createChooser(share, "Share Image"));
4

5 回答 5

2

我的代码有什么问题?

大概什么都没有。ACTION_SEND支持一个 EXTRA_TEXT EXTRA_STREAM一个Intent,而不是两者(“...可以有一个 EXTRA_TEXT 或 EXTRA_STREAM 字段,包含要发送的数据”)。一些ACTION_SEND实现将超出记录的协议并尝试同时使用两者。其他人将坚持记录在案的协议,并且只使用一个。在这种情况下,Facebook 可能选择坚持记录的协议并且只使用一个。

于 2016-04-22T11:00:58.057 回答
2

Facebook will not allow you to Share any static text you want.

That's why Facebook have provide it's own Share Dialog to post any text on facebook.

It will take image as static but not text.

So better for facebook share only you use Facebook Share Dialog.

The thing is, if you put a URL in the EXTRA_TEXT field, it does work. It's like they're intentionally stripping out any text.

**Check below links for Integration of Facebook Share.**

https://developers.facebook.com/docs/sharing/reference/share-dialog

Android:如何通过意图在 Facebook 上共享图像和文本?

于 2016-04-22T11:01:35.107 回答
0

你不能再与 Facebook 分享文字了!期间

于 2016-04-22T11:03:19.500 回答
0

Facebook 改变了他们的政策,所以你不能同时分享图片和文字。再也不可能像以前那样

于 2016-04-22T11:00:17.130 回答
0

不可能使用意图在 facebook 上一起共享图像和文本。但是,如果您想共享两者,则必须创建图像和文本的位图。

从此处下载源代码(使用 android 中的意图在 facebook 上共享图像和文本

活动主.xml:

<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

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


    }
}

谢谢!

于 2017-03-23T08:14:08.877 回答