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
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);

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.

Leave a Reply

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