firnnauriel
firnnauriel

Reputation: 2063

Display byte[] to ImageView in Android

Is it possible to do this? I'm reading an XML file that has the Base64 string of an image. I'm planning to use Base64.decode to have the byte array of the image string. I'm stuck though on how to use it in an ImageView. Do i have to create a 'drawable' class first then set it to ImageView's src property?

Thanks!

Upvotes: 26

Views: 35980

Answers (5)

Code Demon
Code Demon

Reputation: 1333

Late for this solution however i had this i had to share:

 Glide.with(context)
.load(Base64.decode(base_64_string,0))
//.load(Uri.parse(file_uri_string)//load from file path
.diskCacheStrategy(DiskCacheStrategy.NONE)//to prevent caching
.skipMemoryCache(true)//to prevent caching
.into(view);

Upvotes: 0

Tarun Umath
Tarun Umath

Reputation: 910

byte[] pic = intent.getByteArrayExtra("pic"); 
capturedImage = (ImageView) findViewById(R.id.capturedImage);
Bitmap bitmap = BitmapFactory.decodeByteArray(pic, 0, pic.length);
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,capturedImage.getWidth(),capturedImage.getHeight(),true);
capturedImage.setImageBitmap(bitmap1);

Upvotes: 0

Chinmoy
Chinmoy

Reputation: 1476

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

Upvotes: 6

RobCroll
RobCroll

Reputation: 2589

In case anyone else stumbles across this question, here is the code

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

public class ModelAssistant {

    public static void setImageViewWithByteArray(ImageView view, byte[] data) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        view.setImageBitmap(bitmap);
    }
}

Upvotes: 63

Romain Guy
Romain Guy

Reputation: 98531

You can use BitmapFactory.decodeByteArray() to perform the decoding.

Upvotes: 33

Related Questions