Android/ImageButton如何显示二进制字符流的图片
今天做获取用户接口信息,结构如下所示(UserIcon的值只是部分,不可用),有返回用户图片的信息(二进制),需要在页面显示,这里做下笔记
{
"UserCode":"admin",
"UserName":"管理员",
"UserIcon":"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAUCklEQVR4nO2da5RdZX3Gf88+l8kkk2SSyd0"
}
1)直接在控件设置图片来源
<ImageButton android:id="@+id/img_in_pack_user_icon" android:layout_width="80dp" android:layout_height="120dp" android:background="@color/colorWhite" android:src="@drawable/icon_un_login128" android:scaleType="fitCenter" />
2)通过后台动态设置图片来源(安卓图片资源)
ImageButton imgUserIcon = findViewById(R.id.img_in_pack_user_icon);
imgUserIcon.setImageResource(R.drawable.icon_male_doctor128);
3)通过后台动态设置图片来源(二进制字符流转成图片)
3.1)将二进制字符流转成Bitmap方法
public static Bitmap convertStringToIcon(String outPut){ Bitmap bitmap = null; try{ byte[] bitmapArray; bitmapArray = android.util.Base64.decode(outPut, android.util.Base64.DEFAULT); bitmap = BitmapFactory.decodeByteArray(bitmapArray,0,bitmapArray.length); }catch (Exception ex){ ex.printStackTrace(); } return bitmap; }
3.2)调用转换方法并设置图片
String imgStream = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAUCklEQVR4nO2da5RdZX3Gf88+l8kkk2SSyd0QLlEwUDEJKBDABFotWhBB2w8VoZQ54VMXa1VczpilH3TFM12FrsXqJzJDFbVfasW6pF1Yl5jUGC41CYqAWCKYBCYkmdwvM+eyn344MyGTzOScObd9JjnPlydnsvf+v+9+n//zvnvvd79bnEfoSWVbDQuBhYIFofMXSbGLQEvA84E5kjpsz5Uk2x7N7JUYsL0f9C7yTju/S8R2Seyx6ZfU37UhfjLqulYLiroA5aInlY0DUzBzwNci3Yh0lfECoQ5gDpCsUrghYMB4QGgP5hVgs+2tkvYDg1298VyVYtUVk04A6VR2CbBCZrWl6yRWnZ3JdeUtsl404SbQ9q7e+M6oz9FE0P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AAElFTkSuQmCC"; Bitmap bitmap = ToolHelper.convertStringToIcon(imgStream); imgUserIcon.setImageBitmap(bitmap);
参考地址:https://blog.csdn.net/cf8833/article/details/52371491
平时多记记,到用时才能看看,记录你的进步,分享你的成果