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.

[java]
// 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
imageView.setImageBitmap(bmp);

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

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

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.

Author: Rishabh

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