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 convert that to bitmap?
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.
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();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context can be your current Activity.
Here is another way to convert Drawable resource into Bitmap in android:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
First Create Bitmap Image
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
now set bitmap in Notification Builder Icon....
Notification.Builder.setLargeIcon(bmp);
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.
If someone is looking for the Kotlin version for the large icon, you may use this
val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)
Related
I use this code to get a Bitmap object from a bitmap image from drawable resources:
Bitmap avatar = ((BitmapDrawable) getDrawable(R.drawable.avatar_bpm)).getBitmap();
The code works but android studio suggests I use: Use ContextCompat.getDrawable()
What is it mean? And how do I convert my code to use the suggested version ?
I had a image in drawable folder. I want to load using BitmapFactory.decodeFile. What is the file path in this case??
check out this way, provide resources id as a "path"
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image_resource);
I am trying to draw an image from a file, a png file.
I need to draw it as a bitmap on a canvas but i cant get the file into the bitmap object without getting an error, after trying for a lot of time i need some help.
example of what i tried:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.myimage);
thanks!
Which method is more efficient for creating Bitmap out of Drawable from resources?
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
Vs
Drawable myDrawable = getResources().getDrawable(R.drawable.icon_resource);
Bitmap myBitmap = ((BitmapDrawable) myDrawable).getBitmap();
since API 22 above method is deprecated so use following
Drawable myDrawable = ContextCompat.getDrawable(context, R.drawable.icon_resource)
You can take a look at the source code for Bitmap factory at http://source.android.com
specifically the code for decodeResource.
I would reason that using BitmapFactory is preferred but in either case if you are decoding multiple bitmaps then you should call getResources() once and store the result for use as the resources argument for the functions.
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
As per the documentation above method is best when we are constructing bitmap from inputStream
Vs
Drawable myDrawable = ContextCompat.getDrawable(context, R.drawable.icon_resource)
Bitmap myBitmap = ((BitmapDrawable) myDrawable).getBitmap();
This solution is widely used and better in performance as it simply returns the bitmap used by this drawable to render.
Both should have similar decoding performance. In fact, initial creation of the Drawable will call Drawable.createFromResourceStream() which calls BitmapFactory.decodeResourceStream().
However, Resources.getDrawable() and Context.getDrawable() use a Drawable cache, so if you are loading the same Bitmap more than once using this API it can skip decoding if the Drawable is in the cache and performance will be better.
I'm trying to
Drawable d =new BitmapDrawable(bitmap);
not working, I understand that This constructor is deprecated.
advised to use of BitmapDrawable(Resources, Bitmap)
but do not know how to use this constructor
I lay in Resources?
Using context.getResources(). See also http://developer.android.com/reference/android/content/Context.html#getResources()
Following code helped me.
BitmapDrawable bd = new BitmapDrawable(imgview.getResoureces,bitmap);
where imgview is your target ImageView and bitmap is your required Bitmap.