android imageview中未显示Base64图像

时间:2019-01-30 07:49:46

标签: android xamarin.android imageview base64

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

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

2 个答案:

答案 0 :(得分:1)

这是获取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);

答案 1 :(得分:0)

创建将Base64转换为Bitmap的方法

public Bitmap BaseStringToBitmap(string imageBase64)
{
    byte[] imageBytes = Convert.FromBase64String(imageBase64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

比在ImageView对象上使用

imageView.SetImageBitmap(BaseStringToBitmap(base64String));

示例代码

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.activity_main);
    var iv = FindViewById<Android.Widget.ImageView>(Resource.Id.image1);
    iv.SetImageBitmap(BaseStringToBitmap());
}   
public Bitmap BaseStringToBitmap()
{
    var bse64 = "/9j/4ZT8RXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAgAAAA...";
    byte[] imageBytes = Convert.FromBase64String(bse64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

Axml文件

<ImageView
android:id="@+id/image1"
android:background="#89CFF0"    
android:layout_width="200dp" 
android:layout_height="200dp"/>

输出屏幕截图

enter image description here