1

I'm trying this code to send images to WhatsApp, but when it opens the WhatsApp screen and when I select the contact, I don't see any image. Can someone help me?

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.image3);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());

    } catch (IOException e) {
        e.printStackTrace();
    }
    Uri imageUri = Uri.parse(f.getAbsolutePath());
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"));
    //share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///android_asset/image3.jpg"));
    if(isPackageInstalled("com.whatsapp",this)){
        share.setPackage("com.whatsapp");
        startActivity(Intent.createChooser(share, "Share Image"));

    }else{

        Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
    }

}

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
Darga Lael
  • 11
  • 4
  • On which Android version do you try this? If it's from API 24 and above, see this: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en . Sadly, though, the same solution doesn't work for me on VIber, and for some reason, it requires WhatsApp to have storage permission. Please have a look here: http://stackoverflow.com/q/42084954/878126 – android developer Feb 07 '17 at 08:26

0 Answers0