42

What is the difference between setImageBitmap and setImageDrawable?

I have an image which I would like to set dynamically from file. The tutorial that I followed says to convert my Bitmap to a BitmapDrawable then set it using setImageDrawable. I've notice that setting the Bitmap directly with setImageBitmap also works but I don't notice any difference.

Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(image);
imageView.setImageDrawable(bitmapDrawable);

OR

Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(image);
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
meeeee
  • 2,929
  • 3
  • 22
  • 25

5 Answers5

81

There is no difference between the two internally setImageBitmap is calling setImageDrawable.

Below code is picked from ImageView.java of AOSP

public void setImageBitmap(Bitmap bm) {
    // if this is used frequently, may handle bitmaps explicitly
    // to reduce the intermediate drawable object
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • 24
    +1 for looking at the Android source. One of the benefits of open source stuff. – Jochem Aug 17 '12 at 07:51
  • 2
    The difference is between the new BitmapDrawable(Resource, Bitmap) and the deprecated constructor new BitmapDrawable(Bitmap). If you are using the deprecated one a layout problem can happen. – Informatic0re Dec 03 '12 at 14:48
  • 19
    I disagree with the conclusion so. There is a difference : if you want to optimize a list view with images for instance, you would be better creating a Drawable outiside of the UI Thread and assign it to each imageview in the UI thread. If you use a Bitmap, then the creation of the Drawable will slow down the UI thread and the list won't scroll as fast & smooth. – Snicolas Jan 08 '14 at 15:50
18

Actually, you may see the difference, if you sometimes use null as an argument:

imageView.setImageDrawable(null);
Drawable d = imageView.getDrawable(); // d == null

imageView.setImageBitmap(null);
Drawable d = imageView.getDrawable(); // d == some BitmapDrawable, != null

so if you are checking for existence of a drawable in ImageView, beware of it.

Fenix Voltres
  • 3,448
  • 1
  • 29
  • 36
  • 1
    THIS is the most important distinction, thank you for this. I like to use the ImageView's logic for checking if it's set, so this works great. – AutoM8R Aug 04 '14 at 20:31
13

I just wrote up an article for this. Wish to be able to answer your question.

https://plus.google.com/112740367348600290235/posts/VNAfFLDcKrw

ImageView has 4 APIs to specify the image. Which one to use? What is the difference?

  1. setImageDrawable(Drawable drawable)
  2. setImageBitmap(Bitmap bm)
  3. setImageResource(int resId)
  4. setImageURI(URI uri)

ImageView, by the name, is used to display an image. But what is a image? A Bitmap is-a image, not hard to understand and we use setImageBitmap for that purpose. However, internally, the ImageView has-a Drawable but not a Bitmap and that is what setImageDrawable for. When you call setImageBitmap, internally, first the bitmap will be wrapped to BitmapDrawable, which IS-A Drawable, and then call setImageDrawable.

Here is the code.

public void setImageBitmap(Bitmap bm) {
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

So, what about the 3 and 4 API?

You should already know that that are bunches of ways to create a bitmap: from a file path, from an input stream, from the Uri, or from the resource file.

BitmapFactory.decodeFile(String pathName)
BitmapFactory.decodeStream(Inputstream)
BitmapFactory.decodeResource(Resource res, int id)
BitmapFactory.decodeByteArray(byte[] data)

Aware of this, it is easy to understand setImageResource/setImageUri is just same as setImageBitmap.

To sum up, setImageDrawable is the primitive function other APIs rely on. The other 3 are just helper methods making you write less code.

In addition, it is very important to keep in mind that ImageView actually has-a Drawable, which not necessarily to be a BitmapDrawable! You could set any Drawable to the Image view.

Besides setting the Drawable through Java API, you could also using XML attribution to set the source Drawable for ImageView. See example below. Note that the shape could be either an image file (.png, .jpg, .bmp) or xml file.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
  • But which API call is faster? `setImageDrawable` seems to be the fastest but what is with the other ones? – winklerrr Jan 24 '17 at 12:56
4

Both methods are valid and achieve the same result. In the first method you wrap your bitmap around a drawable object which is an abstraction for anything that can be drawn in a View.

The special thing about Drawables is that they let you do all kinds of operations on the graphical object they wrap around (scaling, translation, opacity etc..).

A bitmap is one kind of drawable, you can learn further about drawables here: http://developer.android.com/guide/topics/resources/drawable-resource.html

In the second method, you directly access the bitmap bits without any drawable related operations and simply draw the bitmap as-is on your view.

Hope this helps, cheers, Max.

Max Raskin
  • 1,042
  • 1
  • 14
  • 26
  • So performing operations on the image is the only advantage / difference of converting to a drawable? I accepted nandeesh's answer as it is more applicable for my usage. – meeeee Aug 17 '12 at 08:17
  • Yeah, essentially this is the advantage or rather application of Bitmap drawables. – Max Raskin Aug 17 '12 at 08:35
0
public void setImageBitmap(Bitmap bm) {
// if this is used frequently, may handle bitmaps explicitly
// to reduce the intermediate drawable object
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

maybe the difference is the mContext.getResources(), from mContext.getResources() can get the density of the divice, so the display on sceen is different

lemon
  • 7