【问题标题】:How can I send bitmap to WhatsApp Application如何将位图发送到 WhatsApp 应用程序
【发布时间】:2015-03-06 07:13:24
【问题描述】:

我的问题是如何将位图发送到 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"));

但该代码不起作用。我的错误是什么?谢谢。

【问题讨论】:

标签: android bitmap whatsapp


【解决方案1】:

这对我有用:

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

【讨论】:

  • 对于普通代码,您总是使用代码 sn-ps。代码片段只能用于 HTML 或 javascript 或其他可以在浏览器中运行的代码。您不能在浏览器中运行 Java。以后使用普通代码块...这次我会为您编辑您的答案并修复格式等,但以后请不要再这样做了。这不是我第一次告诉你这件事......
  • 这个需要writeexternalstorage权限,没有怎么办
【解决方案2】:
    //pass your image and text(if you want to share) in this method.
    void shareImage(Bitmap bitmap,String text){
    //bitmap is ur image and text is which is written in edtitext
    //you will get the image from the path
    String pathofBmp=
    MediaStore.Images.Media.insertImage(getContentResolver(),
    bitmap,"title", null);    
    Uri uri = Uri.parse(pathofBmp);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "hello hello"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    相关资源
    最近更新 更多