【问题标题】:How to Share Image+Text using Share Intent in android如何在android中使用共享意图共享图像+文本
【发布时间】:2015-04-28 05:32:15
【问题描述】:

我使用这个代码,但它不能正常工作。

String Body = "Deals Name: " + Bussinessname + "\n"
    + "Deals: " + deal + "\n" + "Address: " + city
    + " ," + country;

Intent share = new Intent(android.content.Intent.ACTION_SEND);

Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + image);
share.setType("image/*");
share.putExtra(Intent.EXTRA_SUBJECT, "The EssexPass");
share.putExtra(Intent.EXTRA_TEXT, Body);
share.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(share, "Share Deal"));

当我使用类型文本时,它可以正常工作。
但我想分享一段文字和一张图片。
并且分享意图在 FaceBook 中不起作用。

【问题讨论】:

标签: android


【解决方案1】:

试试下面的代码:

String fileName = "image-3116.jpg";//Name of an image
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Launcher");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Deal"));

【讨论】:

  • 你试过设置类型和动作吗? @HardikParmar
  • 我没有收到任何错误,但是当我点击 gmail 时,gmail 被强制关闭,并且它不会像那样发送你的文本
  • 抱歉它的工作但不是在 gmail 中的正确方式它的图像没有加载。什么都没有显示,在徒步旅行中只上传了图片......
  • 选择器启动时我没有将 Facebook 视为提供者
【解决方案2】:

使用意图过滤器,我们无法在 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"));


    }
}

【讨论】:

    【解决方案3】:

    您可以在Android Dev 上阅读共享二进制内容,

    示例代码

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
    shareIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
    

    【讨论】:

    • 你没注意到uriToImage,就是你要发送的图片的路径。
    • 可绘制图像和图像 url 怎么样?我们如何分享它?
    • 解释一下drawable imageimage url是什么意思
    【解决方案4】:

    您好,首先您需要在构建 gradle 中添加 compile com.facebook.android:facebook-android-sdk:4.0.0,然后点击此链接。 https://developers.facebook.com/docs/facebook-login/v2.3

    【讨论】:

      【解决方案5】:

      您需要在意图中添加额外内容:

      intent.putExtra("extra_name_1", ”some value”);
      intent.putExtra("extra_name_2", 123);
      intent.putExtra("extra_name_3”, false);
      

      【讨论】:

        【解决方案6】:

        如果你想分享来自 url 的图片text 使用 intent 在您的分享方法中使用以下块

        对于Java

        AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                   try {
                       URL url = new URL("Your url here");
                       HttpURLConnection connection = null;
                       connection = (HttpURLConnection) url.openConnection();
                       connection.connect();
                       InputStream inputStream = null;
                       inputStream = connection.getInputStream();
                       Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
                       Bitmap b = myBitmap;
                       Intent share = new Intent(Intent.ACTION_SEND);
                       share.setType("Image/jpeg");
                       share.setType("text/html");
                       share.putExtra(Intent.EXTRA_TEXT, "shre body");
                       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                       b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                       String path = MediaStore.Images.Media.insertImage(MainActivity.getContentResolver(), b, "Title", null);
                       Uri imageUri = Uri.parse(path);
                       share.putExtra(Intent.EXTRA_STREAM, imageUri);
                       MainActivity.startActivity(Intent.createChooser(share, "Select"));
                   }
                   catch (Exception e){
                       e.printStackTrace();
                   }
                }
            });
        

        kotlin 中:

          AsyncTask.execute {
                        try {
                            val url = URL("Your url here")
                            var connection: HttpURLConnection? = null
                            connection = url.openConnection() as HttpURLConnection?
                            connection!!.connect()
                            var inputStream: InputStream? = null
                            inputStream = connection.inputStream
                            val myBitmap = BitmapFactory.decodeStream(inputStream)
                            val share = Intent(Intent.ACTION_SEND)
                            share.type = "Image/jpeg"
                            share.type = "text/html"
                            share.putExtra(Intent.EXTRA_TEXT, "Text message here")
                            val bytes = ByteArrayOutputStream()
                            myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
                            val path = MediaStore.Images.Media.insertImage(
                                activity?.getContentResolver(),
                                myBitmap,
                                "Title",
                                null
                            )
                            val imageUri = Uri.parse(path)
                            share.putExtra(Intent.EXTRA_STREAM, imageUri)
                            activity?.startActivity(Intent.createChooser(share, "Select"))
                        } catch (e: Exception) {
                        }
        
                    }
        

        在我当时遇到同样问题的旅程中,我使用了这个 谢谢你

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-04
          • 1970-01-01
          相关资源
          最近更新 更多