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
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 have used Picasso to load an image from my company's CDN into a ImageView:
ImageView imgView;
//...
Picasso.with(context).load(Uri.parse(url)).into(imgView);
But now I need load an image as a layout background:
RelativeLayout theLayout;
//...
Picasso.with(context).load(Uri.parse(url)).into(...);
Is it possible with Picasso? If not, should I use a ImageView instead of Relativelayout?
you can use glide to download bitmap and set it as background from any layout.
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Drawable dr = new BitmapDrawable(resource);
theLayout.setBackgroundDrawable(dr);
// Possibly runOnUiThread()
}
});
but the better way is to use imageView on top of relativelayout and make it match_parent and show image on this imageview. this will help you directly use glide or picaso to load image in image view without memory errors.
Picasso.with(getActivity()).load(your url).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
edit:
you may need to override following methods as well
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.e("TAG", "Failed");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.e("TAG", "Prepare Load");
}
}
Yes. You can use Picasso for this. Please check following code :
Picasso.with(getActivity()).load(Uri.parse(url)).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
relativeLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
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 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);
}
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);