Android set bitmap to Imageview(Android 将位图设置为 Imageview)
问题描述
您好,我有一个 Base64 格式的字符串.我想将其转换为位图,然后将其显示为 ImageView.这是代码:
Hi i have a string in Base64 format. I want to convert it ot a bitmap and then display it to an ImageView. This is the code:
ImageView user_image;
Person person_object;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_profile_screen);
    // ImageViews
    user_image = (ImageView) findViewById(R.id.userImageProfile);
    Bundle data = getIntent().getExtras();
    person_object = data.getParcelable("person_object");
    // getPhoto() function returns a Base64 String
    byte[] decodedString = Base64.decode(person_object.getPhoto(), Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    user_image.setImageBitmap(decodedByte);
    }
此代码成功获取 Base64 字符串,我没有收到任何错误.但它不显示图像.可能是什么问题?谢谢
This code get the Base64 String successfully and i do not get any error. But It does not display the image. What can be the problem? Thanks
推荐答案
请试试这个:
byte[] decodedString = Base64.decode(person_object.getPhoto(),Base64.NO_WRAP);
InputStream inputStream  = new ByteArrayInputStream(decodedString);
Bitmap bitmap  = BitmapFactory.decodeStream(inputStream);
user_image.setImageBitmap(bitmap);
                        这篇关于Android 将位图设置为 Imageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 将位图设置为 Imageview
				
        
 
            
        - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 
						
						
						
						
						
				
				
				
				