次のコードを試してみてください。
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するテキストとイメージの両方を共有することができるはずです。また、あなたは、Android M.
参考のためWRITE_EXTERNAL_STORAGE
許可対処する必要があるかもしれませんAndroidManifest.xml
で
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
を::
はあなたが含まれていることを確認し
Download Bitmap from Glide、
Share Image & Text to Whatsappをし、
Get Uri from Bitmap
画像には* .png形式の画像があります。 intent.setType( "image/jpeg")から変更しようとします。 intType.setType( "image/png");それが助けになるかもしれない。 – Konstantin
UriのMIMEタイプが不正です。 'intent.setType(" text/html ");' –