如何将Drawable图像从资源转换为Bitmap

时间:2013-03-06 18:53:23

标签: android image email bitmap drawable

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

我已经尝试了下一个代码:

        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”,这就是一个大问题。

所以我想在尝试将这个Drawable图像转换为Bitmap时,我认为ArrayList必须是Bitmap。我认为我会在附件中定义图像。

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

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

9 个答案:

答案 0 :(得分:49)

有3种方式可以执行转换:

  1. 使用select * from m_room left join order_room on m_room.room_id = order_room.room_id and ('2015-05-16' between start_date and end_date - interval 1 day or '2015-05-20' between start_date - interval 1 day and end_date or start_date between '2015-05-16' and '2015-05-20') where start_date is null;

    设置ImageView
    resource image

    然后从imageView

    获取位图
    imageView.setImageResource(R.drawable.icon);
    
  2. 直接通过Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

    获取可绘制资源
    Resource ID
  3. Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle); 上设置图片,然后将其转换为ImageView(适用于svg / VectorDrawable)

    Bitmap

答案 1 :(得分:25)

Drawable myDrawable = getResources().getDrawable(R.drawable.anImage);
Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();

此外,它可以在带有<bitmap>元素的XML文件中定义。

答案 2 :(得分:7)

这是一段代码,请查看:

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);

答案 3 :(得分:6)

直接方式是:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

如果您在.xml可绘制文件中将其定义为

,则可以更多地配置位图
<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:mipMap=["true" | "false"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

答案 4 :(得分:6)

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

mContext是您的活动背景。

答案 5 :(得分:1)

方法1:您可以直接将其转换为位图

 Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);

方法2:您甚至可以将资源转换为可绘制对象,并从中获得像这样的位图

Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();

对于API> 22 getDrawable方法已移至ResourcesCompat类,以便您执行类似的操作

Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();

答案 6 :(得分:0)

public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
    Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mutableBitmap);
    drawable.setBounds(0, 0, widthPixels, heightPixels);
    drawable.draw(canvas);

    return mutableBitmap;
}
来自https://msol.io/blog/android/android-convert-drawable-to-bitmap/

为我工作。

答案 7 :(得分:0)

就这么简单

type(a.var1[0]) --> str
type(b.var1[0]) --> list

我对矢量图像没有用,但是 png 图像资源工作正常。

答案 8 :(得分:0)

    public Bitmap getBitmap(@DrawableRes final int resId) {
        Drawable drawable = getResources().getDrawable(resId);;
        Canvas canvas = new Canvas();
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }