I am using Picasso to add an icon to my actionbar. I've read that in order to load the image every time I need to make my Target of strong reference and for that I've used final. But, again, it goes into onPrepareLoad and never reaches onBitmapLoaded.
What am I doing wrong?
private void setLogoToActionBar() {
final Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d("DEBUG", "onBitmapLoaded");
Drawable d = new BitmapDrawable(getResources(), bitmap);
actionBar.setIcon(d);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d("DEBUG", "onBitmapFailed");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d("DEBUG", "onPrepareLoad");
actionBar.setIcon(placeHolderDrawable);
}
};
Picasso.with(getContext()).load(mShop.getClientLogo()).placeholder(R.drawable.shop_asset).resize(100, 100).into(target);
}
Related
I have this code to load some images in the adapter. The problem is that most of time it works but sometimes the list was not displayed.
for (Product p: listItem) {
Picasso.get().load( p.getBanner()).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bitmapList.add(bitmap);
notifyDataSetChanged();
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
The problem was because I have used anonymous Target made the object eligible for garbage collection and sometimes it destroyed by Garbage Collector.
I'm using target as callback mechanism (with Picasso).
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// loading of the bitmap was a success
// TODO do some action with the bitmap
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
// loading of the bitmap failed
// TODO do some action/warning/error message
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso
.with(context)
.load(...)
.into(target);
I want to load placeholder if bitmap fails to load, here:
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
My drawable placeholder is located locally in res/drawable folder.
Which is the best way to do the convertion from DRAWABLE to BITMAP?
FIRST WAY, (Alot of people suggests on SO this way):
Bitmap placeholderIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.placeholder);
SECOND WAY, (I think it should be more memory effiecient)
#Override
public void onBitmapFailed(Drawable errorDrawable) {
errorDrawable = getResources().getDrawable(R.drawable.poster_placeholder);
Bitmap placeholderIcon = ((BitmapDrawable) errorDrawable).getBitmap();
}
I am trying to load an image:
Picasso.with(SelectActivity.this).load(picture).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
findViewById(R.id.facebookButton).setEnabled(true);
continueToEditing();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
findViewById(R.id.facebookButton).setEnabled(true);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
picture is a valid string to a reachable, valid JPEG image. I've got everything inside a try/catch block and I've got breakpoints on onBitmapLoaded, onBitmapFailed, and try/catch's catch block.
However, none of this is called. There is also nothing in logcat related to this, too. What am I doing wrong?
Try keeping a strong reference to the Target object as a class variable and give it a try.
E.g.
Target target;// Class variable
//Now define this on your onCreate method
target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
findViewById(R.id.facebookButton).setEnabled(true);
continueToEditing();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
findViewById(R.id.facebookButton).setEnabled(true);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
//Now set the target on the Piccaso load LOC
Picasso.with(SelectActivity.this).load(picture).into(target);
I am trying to load the bitmaps to an arraylist as follows:
Picasso.with(context).load(url).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
photos.add(bitmap);
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
But why is the onBitmapLoaded method never called?
Picasso, doesn't hold strong references to new Target() it will be garbage collected
look over this
I am working on demo application in which I am using Picasso library v2.5.2. It is working fine on all android operating system version, but not in lollipop.
Image whose size is 130KB which is not loading for me. Images whose size is less are loading correctly.
Here is my code for downloading bitmap and set on imageview.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
Picasso.with(this).load(URL).into(target);
I'm not sure what extra stuff I have to do with this so that I will work on lollipop also or this is bug in lib ?
It's a known problem. The problem is that Picasso keeps a weak reference for the Target. To get it working you need to make it strong, by storing a Target as a tag of view, for example.
target = new Target() {
#Override
public void onPrepareLoad(Drawable drawable) {}
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
#Override
public void onBitmapFailed(Drawable drawable) {}
};
imageView.setTag(target);
Picasso.with(this).load(URL).into((Target) imageView.getTag());
EDIT:
I suggest you to use Glide, it's very similar to Picasso, and also recommended by Google. And as you can see in the end of this thread, the original developer solves this BitmapFactory problem by using extra buffer.
Why would you use a Target if you only need to load the image into the ImageView? Just use this:
Picasso.with(this).load(URL).into(imageView, new Callback()
{
#Override
public void onSuccess()
{
//Dimiss progress dialog here
}
#Override
public void onError()
{
//And here
}
});
For documentation look here.
Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView, new Callback() {
#Override public void onSuccess()
{
}
#Override public void onError()
{
}
});
fit() will help you to load image.And use android:adjustViewBounds="true" in your ImageView in xml.