【问题标题】:Android set bitmap to ImageviewAndroid 将位图设置为 Imageview
【发布时间】:2013-02-25 18:13:32
【问题描述】:

您好,我有一个 Base64 格式的字符串。我想将其转换为位图,然后将其显示为 ImageView。这是代码:

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 字符串,我没有收到任何错误。但它不显示图像。 可能是什么问题? 谢谢

【问题讨论】:

  • 请尝试添加这一行:user_image.setScaleType(ScaleType.FIT_XY);
  • 它是否适用于资源图像?比如你写iuser_image.setImageResource(android.R.drawable.ic_delete),会显示什么吗?

标签: java android bitmap imageview


【解决方案1】:

请试试这个:

byte[] decodedString = Base64.decode(person_object.getPhoto(),Base64.NO_WRAP);
InputStream inputStream  = new ByteArrayInputStream(decodedString);
Bitmap bitmap  = BitmapFactory.decodeStream(inputStream);
user_image.setImageBitmap(bitmap);

【讨论】:

  • 感谢您的回复。但它没有用。我想我正确转换为位图。将位图设置为视图后,是否需要重新绘制才能在屏幕上看到?
  • 你能看到那张图片吗?
【解决方案2】:

有一个名为 Picasso 的库可以有效地从 URL 加载图像。它还可以从文件中加载图像。

例子:

  1. 将 URL 加载到 ImageView 中而不生成位图:

    Picasso.with(context) // Context
           .load("http://abc.imgur.com/gxsg.png") // URL or file
           .into(imageView); // An ImageView object to show the loaded image
    
  2. 通过生成位图将 URL 加载到 ImageView 中:

    Picasso.with(this)
           .load(artistImageUrl)
           .into(new Target() {
               @Override
               public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                   /* Save the bitmap or do something with it here */
    
                   // Set it in the ImageView
                   theView.setImageBitmap(bitmap)
               }
    
               @Override
               public void onBitmapFailed(Drawable errorDrawable) {
    
               }
    
               @Override
               public void onPrepareLoad(Drawable placeHolderDrawable) {
    
               }
           });
    

毕加索提供了更多选项。 Here is the documentation.

【讨论】:

    【解决方案3】:
        //decode base64 string to image
        imageBytes = Base64.decode(encodedImage, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        image.setImageBitmap(decodedImage);
    
      //setImageBitmap is imp
    

    【讨论】:

      【解决方案4】:

      此代码适用于我

       ImageView carView = (ImageView) v.findViewById(R.id.car_icon);
      
                                  byte[] decodedString = Base64.decode(picture, Base64.NO_WRAP);
                                  InputStream input=new ByteArrayInputStream(decodedString);
                                  Bitmap ext_pic = BitmapFactory.decodeStream(input);
                                  carView.setImageBitmap(ext_pic);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多