如何发送WhatsApp的意图捕获的图像 [英] How to send the image captured by intent by whatsapp

查看:97
本文介绍了如何发送WhatsApp的意图捕获的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用中,我有一个活动,当用户单击一个按钮时,它会使用意图打开相机,拍照,并在同一活动中将其显示为图像视图,然后还有另一个按钮,可通过Whatsapp发送该照片.**我认为我没有正确保存文件.

In my android app I have activity where when user clicks a button it opens camera by using intent, takes a photo, shows it as a image view in the same activity and after that there is another button for sending that photo by Whatsapp. **I think I am not saving file correctly.

请查看我保存文件并给文件命名的部分,当通过whatsapp按钮发送后,它会打开whatsapp,但没有图像,并且whatsapp会出错.**

Please have look at section where i am saving file and giving file a name and after when send by whatsapp button, it opens up whatsapp but there is no image attached and whatsapp gives error.**

这是我的活动代码.

public class Main2Activity extends AppCompatActivity {

    ImageView iv;
    File imagesFolder;
    File image;
    Uri uriSavedImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        iv = (ImageView) findViewById(R.id.imageView3);
        Button btnCapture = (Button) findViewById(R.id.button_camera);
        Button send = (Button) findViewById(R.id.send_whatsapp);

        //Set listener on Capture button
        btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Implicit Intent
                startActivityForResult(c, 0);
                imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
                imagesFolder.mkdirs(); // <----
                image = new File(imagesFolder, "image_001.jpg");
                String fileName = image.toString();
                Log.e("log for file name", "hello");
                uriSavedImage = Uri.fromFile(image);
                c.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            }
        });


    }

    //Override method onActivityResult used to retreive the image
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap m = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(m);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
    }


    public void sendSelfie(View view) {

        Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
        whatsappIntent.setType("image/jpg");
        whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
        startActivity(Intent.createChooser(whatsappIntent, "Share image using"));
    }


}

我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.happybirthdayprathmesh.Main2Activity">


    <Button
        android:id="@+id/button_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="clickSelfie"
        android:text="Click here to take photo" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="60dp">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

    <Button
        android:id="@+id/send_whatsapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="sendSelfie"
        android:text="Send by Whatsapp" />

</RelativeLayout>

推荐答案

我使用这个:

whatsapp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    File f=new File(muxedvdo);
                    Uri uri = Uri.parse("file://"+f.getAbsolutePath());
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setPackage("com.whatsapp");
                    share.putExtra(Intent.EXTRA_STREAM, uri);
                    share.setType("image/jpeg");
                    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(share, "Share imageFile"));

                }
            });

这篇关于如何发送WhatsApp的意图捕获的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆