如何将Drawable图像从资源转换为位图 [英] How to convert a Drawable image from resources to a Bitmap

查看:81
本文介绍了如何将Drawable图像从资源转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Drawable中的图像附加到电子邮件中(从我的应用程序附加到Gmail应用程序)

I were trying to attach images from Drawable to an email (from my app to Gmail app)

我尝试了下一个代码:

        Intent emailintent2 = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailintent2.setType("image/*");
        emailintent2.putExtra(Intent.EXTRA_EMAIL, emailaddress2);
        emailintent2.putExtra(Intent.EXTRA_SUBJECT, CorAsunto);
        emailintent2.putExtra(Intent.EXTRA_TEXT, message2);

        ArrayList<Uri> uris = new ArrayList<Uri>();
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image1));
        uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image2));

        emailintent2.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        startActivity(emailintent2);

但是当我将图像附加到电子邮件时,我得到的附件没有扩展名".png",那是一个大问题.

But when I attach the image to the email I get the attach without the extension ".png" and thats is a big problem.

因此,我想尝试将此Drawable图像转换为Bitmap,而且我认为ArrayList必须是Bitmap.我想我会在附件中定义图像.

So I think in try to convert this Drawable images to Bitmap and also I think that the ArrayList will have to be Bitmap. I think that I will get the image has image defined in the attachment.

如果可能的话,有人可以告诉我该怎么做吗?转换为位图,添加到Arraylist并附加图像.

If it is possible, can someone tell me how to do it? Convert to Bitmap, add to Arraylist and attach the image.

如果我在我所说的话中说错了,有人可以给我解决方案吗?我需要将Drawable中的图像附加到扩展名为(.png)的电子邮件中.

If I am wrong in all what I said, can someone give me a solution? I need to attach the images from Drawable to the email with the extension (.png).

推荐答案

有3种执行转换的方法:

  1. 使用资源图像

imageView.setImageResource(R.drawable.icon);

然后从imageView获取位图

and then get the bitmap from imageView

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

  • 通过资源ID

    Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle);
    

  • ImageView 上设置图像,然后将其转换为 Bitmap (也适用于svg/VectorDrawable)

  • Set the image on the ImageView then convert it into Bitmap (works for svg/VectorDrawable too)

    ImageView imgView = (ImageView) findViewById(R.id.ImageView);
    imgView.setImageResource(R.drawable.abc_image);
    z.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    

  • 这篇关于如何将Drawable图像从资源转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆