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.
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 ?
my code is this
ll.setBackground(Drawable.createFromPath(new ImageLoader().fullPath+"/desiredFilename.png"));
ll is the object of my linear layout ,is this the correct method
Bitmap bitmap = BitmapFactory.decodeFile(new ImageLoader().fullPath+"/desiredFilename.png");
Resources res=getResources();
BitmapDrawable bitmapDrawable = new BitmapDrawable(res,bitmap);
ll.setBackground(bitmapDrawable);
i also used this code but dosent work
shows error noSuchMethods
try this way
ll.setBackgroundDrawable(bitmapDrawable);
instead of
ll.setBackground(bitmapDrawable);
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 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)
I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:
eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:
Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...
This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).