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

Rob*_*iga 23 email android image 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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

小智 50

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

  1. 设置ImageViewresource image

    imageView.setImageResource(R.drawable.icon);
    
    Run Code Online (Sandbox Code Playgroud)

    然后从imageView获取位图

    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 直接获取可绘制资源 Resource ID

    Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 设置图像ImageView然后将其转换为Bitmap(适用于svg/VectorDrawable)

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


hak*_*iko 29

Drawable myDrawable = getResources().getDrawable(R.drawable.anImage);
Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)

它也可以在带有该<bitmap>元素的XML文件中定义.

  • 这行不通。我得到了ClassCastException:android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable (2认同)

Nip*_*gia 8

这是一段代码,请查看:

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
Run Code Online (Sandbox Code Playgroud)


Mah*_*oud 6

直接的方法是:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Run Code Online (Sandbox Code Playgroud)

如果在.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"] />
Run Code Online (Sandbox Code Playgroud)


Rak*_*esh 6

Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.ic_launcher);
Run Code Online (Sandbox Code Playgroud)

其中mContext是您的活动上下文.