0

I am developing one application in android, in that application i have to get the static map image according to lat n long provided n share it on whatsapp with text. I am using following code snippet but it is showing sharing failed when i try to share it on whatsapp.

    String whatsAppMessage = "Refer this map image";

    Uri uri = Uri.parse("http://maps.google.com/maps/api/staticmap?center=" + latEiffelTower + "," + lngEiffelTower + "&zoom=15&size=200x200&sensor=false");

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_STREAM,uri);
    intent.setType("image/jpeg");
    intent.setPackage("com.whatsapp");
    startActivity(intent);**
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58

1 Answers1

0

Try the following piece of code:

private static final int IMAGE_WIDTH = 600;
private static final int IMAGE_HEIGHT = 400;

private void shareWhatsappImage(Uri imageUri) {
    String pictureText = "Enter your text here";

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setPackage("com.whatsapp");
    shareIntent.putExtra(Intent.EXTRA_TEXT, pictureText);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/jpeg");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    try {
        startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        ex.printStackTrace();
    }
}

private void getBitmap() {
    String urlToShare = "http://maps.googleapis.com/maps/api/staticmap?center=Australia&size=" + IMAGE_WIDTH + "x" + IMAGE_HEIGHT;

    Glide.with(getApplicationContext())
            .load(urlToShare)
            .asBitmap()
            .into(new SimpleTarget<Bitmap>(IMAGE_WIDTH, IMAGE_HEIGHT) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    shareWhatsappImage(getImageUri(MainActivity.this, resource));
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                }
            });
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "DemoImage", null);
    return Uri.parse(path);
}

Just call getBitmap() and you should be able to share both the Text and Image to whatsapp.

Make sure you include:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in AndroidManifest.xml

Also you might need to handle WRITE_EXTERNAL_STORAGE permission for Android M.

References:

Download Bitmap from Glide,
Share Image & Text to Whatsapp and
Get Uri from Bitmap

Community
  • 1
  • 1
Swapnil
  • 31
  • 7