在whatsapp上分享谷歌静态地图图片

时间:2016-05-17 09:16:49

标签: android

我在android中开发一个应用程序,在那个应用程序中我必须根据lat n long获取静态地图图像,并在whatsapp上分享它与文本。我正在使用以下代码片段,但当我尝试在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);**

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

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

只需致电getBitmap(),您就可以将文字和图片分享给whatsapp。

确保包括:

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

中的

此外,您可能需要处理Android M的WRITE_EXTERNAL_STORAGE权限。

参考文献:

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