How to share Image to WhatsApp from Gallery programmatically?
<p>I tried the following code to share an image to WhatsApp. For now, I manually added the image path.</p>

<p>I want to open the gallery when the user clicks a button in my application and he must be able to select an image to share in WhatsApp.</p>

<p>How can I do that?</p>

<p>PS: I need to set the image path dynamically</p>

<pre><code>button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Testing Button", Toast.LENGTH_SHORT).show();

        File f=new File("/sdcard/Download/myimage.jpg");
        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/*");
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        v.getContext().startActivity(Intent.createChooser(share, "Share image File"));
    }
});
</code></pre>