JaneDoe
JaneDoe

Reputation: 39

Getting image from API and setting it in an ImageView

I'm retrieving the data from the API, and the image I'm getting it as a String.

In the API it shows something like this: image: "/9j/4AAQSkZJRgABAQEAYA..."

I'm using this to set it on the ImageView:

byte[] outImage = r.getCover().getBytes();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(theImage);

On a step by step, I saw that theImage is set as null. So at the end I see nothing.

I think the problem is when I parse it to bytes (byte[] outImage = r.getCover().getBytes();)

I'm getting a different data that I get from visual studio, I did a management application to upload the 'resources' and then an API to get the 'resources'. I get [47, 57, 106, 47, 52, ...] instead of [255, 216, 255, 224, 0, ...]

Upvotes: 0

Views: 982

Answers (3)

Tinh Huynh
Tinh Huynh

Reputation: 76

If the string is Base64 format, you have to decode it and pass the result value to Glide. If you are not familiar with Glide, please check out

Glide.with(context)
    .load(Base64.decode(base64ImageString, Base64.DEFAULT))
    .asBitmap()
    .into(imageView);

Upvotes: 1

Ronald Granovski
Ronald Granovski

Reputation: 1

Do you necessarily need to do this in this method?

if not I recommend using Glid.

sample code:

GlideApp.with(context)
    .load("image_Address")
    .into(image view);

more information https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

Upvotes: 0

Patrick
Patrick

Reputation: 117

You could use Picasso. It's easy to implement.

Picasso.with(this).load(yourImageURL).fit().centerInside().into(your_imageView);

Upvotes: 0

Related Questions