Reputation: 5045
I have the problem where I want to use Picasso for my ListView, as it has great bitmap caching, but currently have to simply use imageView.setImageBitmap(bitmap);
.
I have a list of bitmaps which are not downloaded or stored as files, I simply have the Bitmap objects as they were originally from the user's camera or phone storage but at this point the app has placed image effects on each of the bitmaps so that they are unique from the originals, for example:
ArrayList<Bitmap> bitmapList = new ArrayList<>();
Each list item is simply an ImageView that displays one of these bitmaps. When trying to use Picasso for this, I realized I can't simply set an ImageView to a bitmap like the freedom of ImageView.setImageBitmap()
allows me to do, but instead I need the image URI. From what I've seen, the only way to get the URI's from these bitmaps is to store the images on the device first, then extract the URI's from these stored images. The issue with this is it takes a while in the app to save all of these images, then extracting the correct one for each list item is an issue in its own.
Is there any way to use Picasso for ImageViews with just having a bitmap, without saving the image to the device first, similarly to how ImageView.setImageBitmap()
works?
Upvotes: 1
Views: 2637
Reputation: 16728
Picasso has in interface called Target which you can implement. It has 3 methods to implement
https://square.github.io/picasso/javadoc/com/squareup/picasso/Target.html
void onBitmapFailed(android.graphics.drawable.Drawable errorDrawable)
void onBitmapLoaded(android.graphics.Bitmap bitmap, Picasso.LoadedFrom from)
void onPrepareLoad(android.graphics.drawable.Drawable placeHolderDrawable)
onBitMapLoaded will be called when the bitmap has been loaded from a url.
So for example if you implement Target in you current class you can do something like this:
Picasso.with(context).load(some_url).into(this);
then when the bit map has been loaded your implementation of the onBitMapLoaded method will be called. I believe all of Picasso's caching is still in effect, in fact you can even print out where the bitmap came from , memory, disk, file, or network
onBitMapLoaded(Bitmap bitmap, LoadedFrom loadedFrom) {
// do something like maybe
myImageView.setImageBitmap(bitmap);
}
Upvotes: 0
Reputation: 18977
I have the problem where I want to use Picasso for my ListView, as it has great bitmap caching, but currently have to simply use imageView.setImageBitmap(bitmap);
Image caching is good only if you want to load the images from some where except of the RAM which cause take some time, for example if you want to download from internet or reading from file, because accessing those resources is not as fast as the RAM.
ArrayList<Bitmap> bitmapList = new ArrayList<>();
the above code means all of the bitmaps are in the RAM so you do not need any caching mechanism. if you do not have any URI it means you are using RAM. so best solution is not using picasso and use ImageView.setImageBitmap()
Upvotes: 1