Android 如何将可绘制图像从资源转换为位图

Android 如何将可绘制图像从资源转换为位图,android,image,email,bitmap,drawable,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);

我试图将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);
Intent-emailintent2=新的意图(android.content.Intent.ACTION\u-SEND\u-MULTIPLE);
emailintent2.setType(“image/*”);
emailintent2.putExtra(Intent.EXTRA_电子邮件,emailaddress2);
emailintent2.putExtra(Intent.EXTRA_主题,CorAsunto);
emailintent2.putExtra(Intent.EXTRA_TEXT,message2);
ArrayList URI=新的ArrayList();
add(Uri.parse(“android.resource://”+getPackageName()+“/”+R.drawable.image1));
add(Uri.parse(“android.resource://”+getPackageName()+“/”+R.drawable.image2));
emailintent2.putParcelableArrayListExtra(Intent.EXTRA\u流,URI);
星触觉(2);
但是当我将图片附加到电子邮件时,我得到的附加没有扩展名“.png”,这是一个大问题

因此,我想尝试将这个可绘制图像转换为位图,并且我认为ArrayList必须是位图。我想我会得到附件中定义的图像

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

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

Drawable myDrawable=getResources().getDrawable(R.Drawable.anImage);
位图动画图像=((BitmapDrawable)myDrawable).getBitmap();

也可以在XML文件中使用
元素定义它。

这是一段代码,请查看:

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

直接方式是:

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"] />


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

有3种执行转换的方法:

  • 使用
    资源图像设置
    图像视图

    imageView.setImageResource(R.drawable.icon);
    
    然后从imageView中获取位图

    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    
  • 通过
    资源ID

    Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle);
    
  • 图像视图
    上设置图像,然后将其转换为
    位图
    (也适用于svg/VectorDrawable)


  • 从为我工作。

    方法1:您可以像这样直接转换为位图

     Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    
    Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
    
    Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
    
    方法2:您甚至可以将资源转换为可绘制的,从中可以得到如下位图

     Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    
    Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
    
    Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
    
    对于API>22,getDrawable方法移动到ResourcesCompat类,以便执行如下操作

     Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    
    Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
    
    Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
    
    这么简单

    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.some_image)
    

    矢量图像为空,但图像资源工作正常。

    您是否尝试过getBitmap()将标题复制并粘贴到google中:检查最新的APIs这应该比其他解决方案更有效,但我发现getDrawable不受欢迎。这不起作用。我得到
    ClassCastException:android.graphics.drawable.ColorDrawable无法强制转换为android.graphics.drawable.BitmapDrawable
    谢谢。此方法允许从vector drawable获取位图,而其他方法仅从bitmap drawable获取。在发布代码时添加一些注释通常很有用。
        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;
        }