Quick Tip: Android Convert byte[] Data to ImageView

Let’s say you have a byte[] array of image data that you want to convert into an Image by showing up in an ImageView in your layout. We can achieve that with a few lines of code.

// Convert bytes data into a Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView imageView = new ImageView(ConversationsActivity.this);
// Set the Bitmap data to the ImageView

// Get the Root View of the layout
ViewGroup layout = (ViewGroup) findViewById(android.R.id.content);
// Add the ImageView to the Layout

We convert our byte data into a Bitmap using Bitmap.decodeByteArray() and then set that to a newly created ImageView.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Finally the ImageView is appended to the relevant layout. If you notice android.R.id.content is a quick way to get access to the root element of our layout which is the view group.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

One thought on “Quick Tip: Android Convert byte[] Data to ImageView”

  1. Hi Rishabh,

    Pretty straight forwards and it works like a charm. But I have a problem and I can’t seem to find the correct sollution, I’ve been looking for days now. The BitmapFactory obviously returns a bitmap. The problem is, bitmaps don’t support transparency, and I need this for my icons etc. So I want to use png instead. Do you have any idea how to convert a byte array to png in stead of bitmap?
    The byte array comes from a string I create on a asmx webservice and I use Base64 coding. This works perfectly, but I wan’t to be able to convert it to png. Or maybe there’s a better sollution for getting png images in my app (they will have to come from my server, so not installed with the app).
    I really hope you can help me.

Leave a Reply

Your email address will not be published. Required fields are marked *