0

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.

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

Here is my activity code.

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


}

My XML Code

<?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>
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
BeeBee8
  • 2,944
  • 1
  • 27
  • 39

5 Answers5

0

I think this Link help you for better understanding How to send Images to whatsupp by using Intent.

I Hope you doing well

Community
  • 1
  • 1
Vishal Senjaliya
  • 454
  • 6
  • 21
0

Try this:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.setPackage(“com.whatsapp");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ imagePath));
Saeid
  • 4,147
  • 7
  • 27
  • 43
Ashish
  • 183
  • 9
  • I am not having problem invoking whatsapp intent, I am having problem with image path. Even if I mention image path, i am not getting image in the whatsapp. please see my code and let me know if i am giving correct image path which was generated in the activity itself. what exactly i am supposed to enter in ("file://"+ imagepath) – BeeBee8 Sep 11 '16 at 05:13
  • Uri.parse("file://"+image.getAbsolutePath()); – Ashish Sep 11 '16 at 07:13
0

Below code will give a generic way of sharing image and text to whats app and other similar apps.

public void shareShop(Activity activity, String imagePath, String url) {
        Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
        whatsappIntent.putExtra(Intent.EXTRA_TEXT, url);
        if (imagePath != null) {
            whatsappIntent.setType("image/*");
            whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imagePath));
        }//add image path
        else {
            whatsappIntent.setType("text/*");
        }
        activity.startActivity(Intent.createChooser(whatsappIntent, "Share image using"));
        try {
            activity.startActivity(whatsappIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
        }
    }
CodingRat
  • 1,934
  • 3
  • 23
  • 43
0

String imagePath = "file://" + image.getAbsolutePath(); Uri.parse(imagePath)

Ashish
  • 183
  • 9
0

I use this :

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

                }
            });
Saurav Prakash
  • 588
  • 1
  • 5
  • 26