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 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.