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 ?
Related
I'm making an app that take datas from a database and it should take an ImageIcon. How can I use that class in Android and convert it to Drawable to show it in app?
It's not from a resource file. It's from an ImageIcon as variable of an object i receive from database.
try this
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.png");
Drawable d = new BitmapDrawable(getResources(), bitmap);
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 using Robolectric to test android code, and found there are some tests failed because there are some methods don't have shadow method.
In my logic code, I retrieved a bitmap from another activity:
Bitmap bitmap = getBitmapFromResult(data);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
And later in another method, I get the bitmap from the imageView, and save it to file:
BitmapDrawable drawable = (BitmapDrawable)imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// save to file
bitmap.compress(...);
But the last line throws NullPointerException when testing.
I debugged into the code, and found when the test code running with robolectric, the drawable is a ShadowBitmapDrawable contains a null bitmap. So drawable.getBitmap() return a null which causes the exception.
Then I enabled logging in my test code:
Robolectric.logMissingInvokedShadowMethods();
Found one line in console which I think causes my test code failed:
No Shadow method found for BitmapDrawable.<init>(android.content.res.Resources, android.graphics.Bitmap)
What can I do now? Is it possible to add a shadow method to fix it and how to do that?
Clone the source from github: https://github.com/pivotal/robolectric/
Add a shadow method to ShadowBitmapDrawable.java:
public void __constructor__(android.content.res.Resources res, Bitmap bitmap) {
this.bitmap = bitmap;
}
Then compile it and package it to a jar. It's fixed.
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 want to retrieve an image from my data/data/com.apps.myapp/images folder and display it in an ImageView. Any clue?
Try this :
Bitmap bitmap = BitmapFactory.decodeFile("data/data/com.apps.myapp/images/img.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
There are several components involved in this.
To get the path of your data folder you can use the method getDir in the Context.
Now you have to know the file name and open an stream here again the Context class is your friend. Now the stream can be decoded into a Bitmap via a Bitmap Factory.
After you got a Bitmap create a BitmapDrawable from it and pass it to your ImageView