我的问题是如何将位图发送到Whatsapp应用程序,我使用下面的代码;
ImageView iv=(ImageView)view.findViewById(R.id.item_image);
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.setType("image/png");
waIntent.putExtra(Intent.ACTION_SEND, byteArray);
startActivity(Intent.createChooser(waIntent, "Share with"));
但是该代码不起作用。我的错误是什么?谢谢。
最佳答案
这为我工作:
public void onClickApp(String pack, Bitmap bitmap) {
PackageManager pm = context.getPackageManager();
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
Uri imageUri = Uri.parse(path);
@SuppressWarnings("unused")
PackageInfo info = pm.getPackageInfo(pack, PackageManager.GET_META_DATA);
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.setPackage(pack);
waIntent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri);
waIntent.putExtra(Intent.EXTRA_TEXT, pack);
context.startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (Exception e) {
Log.e("Error on sharing", e + " ");
Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show();
}
}