-2

I want to send an image to WhatsApp with text but when I click to send the app gets crashed...

Here is my code...

public void onClick(View view) {

                BitmapDrawable bitmapDrawable = ((BitmapDrawable) whatsapp.getDrawable());
                Bitmap bitmap = bitmapDrawable.getBitmap();
                String bitpath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "WhatsApp", null);

                Uri uri = Uri.parse(bitpath);
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.setType("image/png");
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.putExtra(Intent.EXTRA_TEXT, "Hey..");
                chooser = Intent.createChooser(shareIntent, "Send Image..");
                startActivity(chooser);
            }
Tarun Pandat
  • 109
  • 1
  • 9
  • 1
    If by "the app stop working", you mean it's crashing, please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Dec 02 '19 at 06:55
  • Ya, it's crashing, I edited my question. – Tarun Pandat Dec 02 '19 at 06:57

3 Answers3

0

Can you try this code for sharing in Whats app?

  uri1=Uri.parse(Paths+File.separator+Img_name);
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("image/*");
                //intent.putExtra(intent.EXTRA_SUBJECT,"Insert Something new");
                String data = "Hello";
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putExtra(Intent.EXTRA_TEXT,data);
                intent.putExtra(Intent.EXTRA_STREAM,uri1);
                intent.setPackage("com.whatsapp");

                startActivity(intent);
Vinay Jayaram
  • 1,030
  • 9
  • 29
0

You can try below code for sharing Image with Text to Whatsapp. First you have to create xml folder under res and named as you like (file_paths)

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/package_name/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>

Now inside Menifest.xml, add below code under application tag

<provider
 android:name="android.support.v4.content.FileProvider"
 android:authorities="package_name.fileProvider"
 android:exported="false"
 android:grantUriPermissions="true">
 <meta-data
 android:name="android.support.FILE_PROVIDER_PATHS"
 android:resource="@xml/file_paths" />
 </provider>

After that, create a method inside you Java file, you have to pass File in this method

private void shareImageToWhatsapp(File imageFile) {
    String text = String.format("Enter your message here");
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setPackage("com.whatsapp");
    share.setType("image/*");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Uri contentUri = FileProvider.getUriForFile(getContext(), "package_name.fileProvider", imageFile);
    share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    share.putExtra(Intent.EXTRA_SUBJECT, "App Name");
    share.putExtra(Intent.EXTRA_TEXT, text);
    share.putExtra(Intent.EXTRA_STREAM, contentUri);
    startActivity(Intent.createChooser(share, "Share Image"));
}

Hope this will help you.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
  • I'm new on android I need a simple way that I can understand please give me some code that I can understand, Sorry... – Tarun Pandat Dec 02 '19 at 07:24
  • There is only 3 steps, 1 create new folder in res-->xml and create a file as file_paths 2. Add code in Menifest.xml 3. Add method in Java file and call this method on button click – Sanwal Singh Dec 02 '19 at 07:26
0

following code worked for me...

 button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Insert Subject here");
            // String app_url = " https://play.google.com/store/apps/details?id=my.example.javatpoint";
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, imgUrl);
            startActivity(Intent.createChooser(shareIntent, "Share via"));


        }
    });
Wini
  • 115
  • 14