Android set bitmap to Imageview

2020-05-30 08:40发布

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);
    }

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

4条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-30 09:03

Please try this:

byte[] decodedString = Base64.decode(person_object.getPhoto(),Base64.NO_WRAP);
InputStream inputStream  = new ByteArrayInputStream(decodedString);
Bitmap bitmap  = BitmapFactory.decodeStream(inputStream);
user_image.setImageBitmap(bitmap);
查看更多
Deceive 欺骗
3楼-- · 2020-05-30 09:07
    //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楼-- · 2020-05-30 09:12

this code works with me

 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);
查看更多
Rolldiameter
5楼-- · 2020-05-30 09:25

There is a library named Picasso which can efficiently load images from a URL. It can also load an image from a file.

Examples:

  1. Load URL into ImageView without generating a bitmap:

    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. Load URL into ImageView by generating a bitmap:

    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) {
    
               }
           });
    

There are many more options available in Picasso. Here is the documentation.

查看更多
登录 后发表回答