30

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

I have tried the next code:

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

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

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.

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

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).

4

9 回答 9

58

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

  1. 设置ImageViewresource image

    imageView.setImageResource(R.drawable.icon);
    

    然后从 imageView 获取位图

    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    
  2. 直接获取drawable资源Resource ID

    Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.profile_circle);
    
  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());
    
于 2015-05-13T06:35:06.097 回答
34
Drawable myDrawable = getResources().getDrawable(R.drawable.anImage);
Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();

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

于 2013-03-06T19:03:38.133 回答
9

这是一段代码,只需检查一下:

Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
于 2013-03-06T19:12:57.787 回答
6
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.ic_launcher);

mContext 是您的活动上下文。

于 2015-02-06T09:31:18.630 回答
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"] />
于 2015-01-29T08:48:30.773 回答
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();
于 2020-01-04T11:04:12.410 回答
1
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/为我工作。

于 2019-03-14T11:46:21.707 回答
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;
    }
于 2020-12-11T13:49:05.047 回答
-1

就这么简单

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

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

于 2020-02-07T08:20:40.690 回答