如何将位图发送到WhatsApp应用程序

How can I send bitmap to WhatsApp Application(如何将位图发送到WhatsApp应用程序)
本文介绍了如何将位图发送到WhatsApp应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何将位图发送到WhastApp应用程序,并使用以下代码;

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

这篇关于如何将位图发送到WhatsApp应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)