Reputation: 41
I'm trying to convert base64 image into bitmap image and then load it using picasso library into a recycler view. However, I get an error whenever I run my code saying I need to pass in a URI into the picasso method.
public String getImage(){
Context context =null;
byte[] decodedString = Base64.decode(image, Base64.URL_SAFE);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
decodedByte.compress(Bitmap.CompressFormat.PNG, 100, bytes);
path = MediaStore.Images.Media.insertImage(context.getContentResolver(), decodedByte, null, null);
return Uri.parse(path);
}
DataAdapter:
Picasso.with(context).load(data.get(i).getImage()).into(holder.reportImage);
Upvotes: 1
Views: 3059
Reputation: 435
Assuming that you are using base64 image as a placeholder for your item in recycler view I suggest you use this method to convert base64 string to bitmap -
fun fromBase64ToBitmap(context: Context, base64string: String?): BitmapDrawable {
val decodedString = Base64.decode(string, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
return BitmapDrawable(context.resources, bitmap)
}
and set
picasso.load(your_high_quality_image)
.placeholder(bitmapDrawableFromAboveMethod)
.into(imageHolder)
Hope this helps.
Note: Code is in Kotlin but you can easily convert it in java
Upvotes: 1
Reputation: 10621
Once you have downloaded and decoded a bitmap, there is no point of using an image loading library. All of the benefits (request queue and cache) are lost. You can simply use holder.reportImage.setImageBitmap(bmp);
Another approach would be to write a custom RequestHandler
, that will decode the base64 data after downloading.
Upvotes: 1