Can anyone point me towards an example of how one might change an XML layout's background programatically using Picasso? All the examples I've found are able to update an ImageView using Picasso - but not a layout background.
Unable to set LinearLayout BackgroundResource from URL using Picasso
You can use Picasso's Target:
Picasso.with(this).load("http://imageUrl").into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mYourLayout.setBackground(new BitmapDrawable(bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
UPDATE
As #BladeCoder mentioned in the comment, Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected.
So, following Jake Wharton's comment on one of the issues, i think this could be a good way to go:
CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
Picasso.with(this).load("http://imageUrl").into(mCustomLayout);
CustomLayout.java:
public class CustomLayout extends LinearLayout implements Target {
public CustomLayout(Context context) {
super(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setBackground(new BitmapDrawable(getResources(), bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
//Set your error drawable
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//Set your placeholder
}
}
I use an ImageView as temporary ImageHolder. At first, load image with picasso into ImageView and set Layout Background from this ImageView by using getDrawable.
ImageView img = new ImageView(this);
Picasso.with(this)
.load(imageUri)
.fit()
.centerCrop()
.into(img, new Callback() {
#Override
public void onSuccess() {
myLayout.setBackgroundDrawable(img.getDrawable());
}
#Override
public void onError() {
}
});
None of the solutions above worked for me. But #Thiha's solution was the closest. The below worked for me:
final ImageView img = new ImageView(this);
Picasso.with(img.getContext()).load(url).into(img, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
collapsingToolbarLayout.setBackgroundDrawable(img.getDrawable());
}
#Override
public void onError() {
}
});
In my case i needed the image to fit the imageview's size so instead of loading the image in background i added this property to the imageview and loaded the image normally.
android:scaleType="fitXY"
As an alternative to previous answers. It's possible to set background via Piccasso simply by creating ImageView with match parent on the layout.
XML:
<ImageView
android:id="#+id/imageView_background"
android:layout_width="match_parent"
android:contentDescription="#string/background"
android:layout_height="match_parent"
android:alpha="0.7"/>
Kt:
Picasso.get().load(R.drawable.background_green_forest).fit().centerCrop().into(view.imageView_background)
You can try this, it works fine for me.
//ihave used releative layout in my activity
private RelativeLayout chatBG;
//initialize your layout
chatBG = findViewById(R.id.chat_activity);
//using picasso library, its better to use it on onStart() method to avoid some bugs
Picasso.get().load("https://your/url")
.placeholder(R.drawable.yourplaceholder_img)
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
chatBG.setBackground(new BitmapDrawable(bitmap));
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(ChatActivity.this, "Error : loading wallpaper", Toast.LENGTH_SHORT).show();
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
Related
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 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);
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.
I am using Picasso library to load image from url. The code I used is below.
Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder).into(imageView);
What I wanna do is to get the image that loaded from url. I used
Drawable image = imageView.getDrawable();
However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.
Thanks in advance.
This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
Drawable image = imageView.getDrawable();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(this).load("url").into(target);
mImageView.post(new Runnable() {
#Override
public void run() {
mPicasso = Picasso.with(mImageView.getContext());
mPicasso.load(IMAGE_URL)
.resize(mImageView.getWidth(), mImageView.getHeight())
.centerCrop()
.into(mImageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Drawable drawable = mImageView.getDrawable();
// ...
}
#Override
public void onError() {
// ...
}
});
}
});