Mark Joshua Fajardo
Mark Joshua Fajardo

Reputation: 142

Set a bitmap image in ImageView

I've tried this

try {
        byte[] decodedString = Base64.decode(repPlus, Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        holder.imageView.setImageBitmap(decodedByte);
    } catch (Exception e) {
        Log.d("Error: ", e.getMessage());
    }

And it returns an error:

bad base-64

And then I've tried this Bad base-64 error. And it removes the error. And Base64 to Bitmap to display in ImageView for decoding a base64 String

try {
        String repSlash = product.getImage().replace("/", "_");
        String repPlus = repSlash.replace("+", "-");
        byte[] decodedString = Base64.decode(repPlus, Base64.URL_SAFE);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        holder.imageView.setImageBitmap(decodedByte);
    } catch (Exception e) {
        Log.d("Error: ", e.getMessage());
    }

But the image does not views in the application.

Upvotes: 0

Views: 179

Answers (1)

Thawindu Angesh Tuto
Thawindu Angesh Tuto

Reputation: 392

Try doing this after initializing the Bitmap decodedByte

BitmapDrawable drawable = new BitmapDrawable(getResources(), decodedByte);
holder.imageView.setBackgroundDrawable(drawable);

EDIT: Try this:

String base64Image = product.getImage().split(",")[1];

byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

holder.imageView.setImageBitmap(decodedByte);

Hope this helped!

Upvotes: 2

Related Questions