I'm using the Picasso framework to handle image loading in my Android app.
After the image is loaded, I need to access the Drawable to apply some masking operations. The issue is that Picasso converts the Drawable to a PicassoDrawable, and a simple cast back to Drawable does not work.
This is the code I have:
Picasso.with(mContext).load(image.getPath()).into(mImageView, new Callback() {
#Override
public void onSuccess() {
Util.applyMask(imageView);
}
#Override
public void onError() {
}
});
and the Util.applyMask(ImageView) method:
public static void applyMask(ImageView imageView) {
// this is where a class cast exception happens since it's actually a PicassoDrawable and not a Drawable
Bitmap mainImage = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
// ...
}
A possible solution is given by Jake Wharton in this github issue: https://github.com/square/picasso/issues/38
To sum up, the solution is: "If you want access to the Bitmap directly then you'll need to use the Target callbacks. The PicassoDrawable is used to allow fading and the debug indicator."
I'm not exactly sure how to access the Target callback. Anyone knows how to solve this?
Thanks.
This was answered at github (https://github.com/square/picasso/issues/38):
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
private void loadBitmap() {
Picasso.with(this).load("url").into(target);
}
Related
Before you set the question/issue as duplicate, please read it all first.
I know it is a known issue and there's loads of question on Stackoverflow and issues on Github but believe me I tried them all.
ISSUE
Not loading this link:
https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13872950_1066865640060722_8272182690153279858_n.jpg?oh=66a4ff80019c1fbf79bee45d32f03468&oe=59F65F50
MY CODE
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Resources resources = getContext().getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_image_content_error);
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Resources resources = getContext().getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_image_placeholder);
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
};
Picasso.with(getContext()).load(me.getPicture().getUrl()).into(target);
WHAT I HAVE TRIED
NO CACHE:
Picasso.with(getContext()).load(me.getPicture().getUrl()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(target);
NEW DOWNLOADER:
`new Picasso.Builder(getContext()).downloader(new OkHttpDownloader(getContext())).build().load(me.getPicture().getUrl()).into(target);`
BOTH:
`new Picasso.Builder(getContext()).downloader(new OkHttpDownloader(getContext())).build().load(me.getPicture().getUrl()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(target);`
What am I doing wrong?
Ok so the question was answered here:
https://github.com/square/picasso/issues/1658
My mistake was creating the Target as a local method property then when Picasso took a little bit more time to load the image maybe the Garbage Collector cleaned the Target reference which was make it impossible to Picasso to load it into the Target. That is why it SOMETIMES it worked.
SOLUTION
Create Target object as a global property inside the Activity to hold its reference for as long as you use the imageView you want to load the image into. That fixed the issue. :)
Thanks guys!
Does anyone know how to download an image once then use the same Picasso instance to load into multiple ImageView? Right now I'm using a (pretty bad) workaround like the one below, in order to be sure the image is already cached and not downloaded again.
Picasso.with(container.getContext()).load(photo.getPath()).placeholder(R.drawable.placeholder_outfit).fit().centerCrop().into(image1, new Callback() {
#Override
public void onSuccess() {
Picasso.with(container.getContext()).load(photo.getPath()).placeholder(R.drawable.placeholder_outfit).fit().centerCrop().into(image2);
}
#Override
public void onError() {
}
});
You can do it like this:
Picasso.with(container.getContext())
.load(photo.getPath())
.placeholder(R.drawable.placeholder_outfit)
.fit()
.centerCrop().into(image1, new Callback() {
#Override
public void onSuccess() {
imageView2.setImageDrawable(image1.getDrawable()); //Get the ImageView's image (this won't download it, it will get the downloaded image) and set it to your second imageView.
}
#Override
public void onError() {
}
});
To use centerCrop(), just add:
imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
I'm using Glide to load image into a ViewPager using an PagerAdapter.
When I load the images using the following method:
Glide.with(mContext).load(mImage).placeholder(R.drawable.placeholder).into(mImageView);
Everything works fine, but now I need to get the bitmap from glide and store it in a map when it loads for future edit, so I switched this method to the following one:
Glide.with(mContext).load(mImage).asBitmap().placeholder(R.drawable.placeholder).into(new SimpleTarget<Bitmap>() {
#Override
public void onLoadStarted(Drawable placeholder) {
super.onLoadStarted(placeholder);
mImageView.setImageDrawable(placeholder);
}
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
mBitmapMap.put(position, bitmap);
mInterface.onImageLoaded(position, bitmap);
}
});
But the result is that the image not always shown. I think it's some how related to the fact the glide loads images async and at some point it returns when instatiateItem method already finished running.
It looks like this question is related. But the suggestions there did not help me. Has someone encountered this problem and has a solution for it?
The solution for this issue was to use another kind of target, instead of using the SimpleTarget object which I was using when I wrote the question I replaced it with the BitmapImageViewTarget object which I guess handles images asynchronously better. So the final code that I used for it is:
Glide.with(BaseApplication.getInstance()).load(newContent).asBitmap().placeholder(R.drawable.ic_action_picture).into(new BitmapImageViewTarget(mIvContent) {
#Override
public void onLoadStarted(Drawable placeholder) {
super.onLoadStarted(placeholder);
mIvContent.setImageDrawable(placeholder);
}
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
super.onResourceReady(resource, glideAnimation);
mBitmapMap.put(position, resource);
progressBar.setVisibility(View.INVISIBLE);
mIvContent.setImageBitmap(resource);
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
progressBar.setVisibility(View.INVISIBLE);
}
});
From the UX point of view, it will be great to show the user a thumbnail first until the real image completes loading, then showing it to him, but Picasso uses only a resource file as the place holder like:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.into(imageView);
So, how can I use a thumbnail URL as the placeholder? , and if I should use Picasso twice, then how?
An issue is already opened on Picasso's github page with this request, but seems it won't be added to Picasso as per JakeWharton. So how could we do it with what's available in hand?
Thanks to raveN here & the comments on the original request on github, finally I've got a working solution:
Picasso.with(context)
.load(thumb) // thumbnail url goes here
.into(imageView, new Callback() {
#Override
public void onSuccess() {
Picasso.with(context)
.load(url) // image url goes here
.placeholder(imageView.getDrawable())
.into(imageView);
}
#Override
public void onError() {
}
});
The trick here is to get the drawable from the imageView (which is the thumbnail) after the first call & pass it as a placeholder to the second call
-- update --
I've made a blog post describing the whole scenario
You could write a simple helper which calls Picasso twice (as you mentioned).
I've not tested it, but it should go like
Picasso.with(context)
.load(thumbnailUrl)
.error(errorPlaceholderId)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
// TODO Call Picasso once again here
}
#Override
public void onError() {
}
);
There are a couple of different ways to get your Picasso called twice. One method I could think of (again, not tested) is
public static void loadImageWithCallback(String url, Callback callback) {
Picasso.with(context)
.load(url)
.error(errorPlaceholderId)
.into(imageView, callback);
}
public static void loadImage(String url) {
Picasso.with(context)
.load(url)
.error(errorPlaceholderId)
.into(imageView);
}
loadImageWithCallback("http://example.com/mythumbnail.jpg", new Callback() {
#Override
public void onSuccess() {
loadImage("http://example.com/myRealImage.jpg");
}
#Override
public void onError() {
}
}
Edit: All I know is that Picasso provides this callback mechanism. I'm using it in my app to hide a ProgressBar that is displayed until the image is loaded. I'll hide it in success or error callbacks - so you'll have the option to get notified when image loading is done. Then you can simply call it again. I hope the above approach works.
I originally used AbdelHady's solution but found that the larger image is only loaded after the thumbnail is done loading so I came up with this instead.
Assuming you have a utility class in your project;
public final class U {
public static void picassoCombo(final RequestCreator thumbnail,
final RequestCreator large,
final ImageView imageView) {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
imageView.setImageDrawable(errorDrawable);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
thumbnail.into(imageView);
}
};
imageView.setTag(target); // To prevent target from being garbage collected
large.into(target);
}
}
Usage:
U.picassoCombo(
Picasso.with(context)
.load("http://lorempixel.com/200/100/sports/1/")
.placeholder(R.drawable.ic_image_placeholder),
Picasso.with(context)
.load("http://lorempixel.com/800/400/sports/1/")
.error(R.drawable.ic_image_broken),
imageView
);
In the above example the placeholder is set first, the thumbnail url is set next, and regardless of whether the thumbnail request is done, successful, or failed, the large image request is set once it is done. If the large image request failed, then the error drawable is set.
The only issue is that if you use setIndicatorsEnabled(true) the debug indicators don't show for the large request. As far as I can tell this seems to be by design according to this issue convo
I am using Picasso library to load images stored on my server to my android application.
I am using the normal code to do this.
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
but now i want to set this image as a backgroud to my listview with id = myList .
Any help would be appreciated.
Thank you. :D
You could try to override the new target() implementation to set your view.
Picasso.with(context).load(url).into(new Target() {
#Override public void onSuccess(Bitmap bitmap) {
// Set imageview bitmap here.
// Do other stuff.
}
#Override public void onError() {
}
});
Please take note that the above won't work inside a ListView unless you implement hashCode/equals in your Target.
Implement the Target class.
Pseudo code:
Picasso.with(context).load(...).into(
new Target() {
public void onLoaded(Bitmap bitmap, Picasso.LoadedFrom from){
mListView.setBackground(bitmap);
}
/* ... */
}
);
Note that this code will not compile, as I don't know the exact API's, but this will help you further.