开发者

Display byte[] to ImageView in Android

Is it possible to do this? I'm rea开发者_运维知识库ding 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!


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


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜