首页 > 解决方案 > Base64图像未显示在android imageview中

问题描述

我试图在图像视图中显示 base64 图像,但它不显示。我正在解码和设置图像视图的图像位图。我正在动态添加图像视图。问题是因为我动态添加图像吗?下面是代码片段:

LayoutInflater inflaterDocuments = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View inflatedHeaderLayout = inflaterDocuments.Inflate(Resource.Layout.imageListItem, null, false);
ImageView imageView = inflatedHeaderLayout.FindViewById<ImageView>(Resource.Id.imageView);
byte[] decodedString = Base64.Decode(base64String, Base64Flags.Default);
Bitmap bitMap = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);
imageView.SetImageBitmap(bitMap);
imageView.Invalidate();
_imageLayout.AddView(inflatedHeaderLayout);

编辑

Base64 字符串文件Base64.txt

标签: androidxamarin.androidimageviewbase64

解决方案


这是获取base64的方法

public static String getBase64String(Bitmap image) {
    String encodeString = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        encodeString = Base64.encodeToString(byteArray, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encodeString;
}

滑行用于显示图像

String photoId = base64;
    Glide.with(getApplicationContext())
            .load(photoId)
            .apply(RequestOptions.circleCropTransform())
            .into(employeeImage);

推荐阅读