converting drawable resource image into bitmap

前端 未结 6 1827
星月不相逢
星月不相逢 2020-12-02 05:22

I am trying to use the Notification.Builder.setLargeIcon(bitmap) that takes a bitmap image. I have the image I want to use in my drawable folder so how do I con

相关标签:
6条回答
  • 2020-12-02 05:38

    Here is another way to convert Drawable resource into Bitmap in android:

    Drawable drawable = getResources().getDrawable(R.drawable.input);
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    
    0 讨论(0)
  • 2020-12-02 05:43
    Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
    

    Since API 22 getResources().getDrawable() is deprecated, so we can use following solution.

    Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo,  getContext().getTheme());
    Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
    
    0 讨论(0)
  • 2020-12-02 05:55

    In res/drawable folder,

    1. Create a new Drawable Resources.

    2. Input file name.

    A new file will be created inside the res/drawable folder.

    Replace this code inside the newly created file and replace ic_action_back with your drawable file name.

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/ic_action_back"
        android:tint="@color/color_primary_text" />
    

    Now, you can use it with Resource ID, R.id.filename.

    0 讨论(0)
  • 2020-12-02 05:56

    First Create Bitmap Image

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

    now set bitmap in Notification Builder Icon....

    Notification.Builder.setLargeIcon(bmp);
    
    0 讨论(0)
  • 2020-12-02 05:59
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    

    Context can be your current Activity.

    0 讨论(0)
  • 2020-12-02 06:02

    You probably mean Notification.Builder.setLargeIcon(Bitmap), right? :)

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
    notBuilder.setLargeIcon(largeIcon);
    

    This is a great method of converting resource images into Android Bitmaps.

    0 讨论(0)
提交回复
热议问题