Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a Color as placeholder image with Picasso?

I want to use Picasso to set a Color as placeholder image.

I tried this:

    int placeHolderColor2 = Color.rgb(20,20,20);

    Picasso.with(context)
         .load(item.getImageUrls().get(0))
         .placeholder(placeHolderColor2)
         .error(R.drawable.card_image)
        .centerCrop()
        .fit()
        .into(viewHolder.imageView);

But it leads to the following error:

10-07 05:36:42.965 5827-5827/? E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0xff141414
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.content.res.Resources.getValue(Resources.java:1266)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.content.res.Resources.getDrawable(Resources.java:785)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.content.res.Resources.getDrawable(Resources.java:752)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.squareup.picasso.RequestCreator.getPlaceholderDrawable(RequestCreator.java:676)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.squareup.picasso.RequestCreator.into(RequestCreator.java:637)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.lorentzos.swipecards.ServiceCardDtoListAdapter.createViewFromResource(ServiceCardDtoListAdapter.java:116)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.lorentzos.swipecards.ServiceCardDtoListAdapter.getView(ServiceCardDtoListAdapter.java:66)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.lorentzos.flingswipe.SwipeFlingAdapterView.layoutChildren(SwipeFlingAdapterView.java:161)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.lorentzos.flingswipe.SwipeFlingAdapterView.refresh(SwipeFlingAdapterView.java:152)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at com.lorentzos.flingswipe.SwipeFlingAdapterView.onLayout(SwipeFlingAdapterView.java:138)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.View.layout(View.java:15671)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5038)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.View.layout(View.java:15671)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5038)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.View.layout(View.java:15671)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5038)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.View.layout(View.java:15671)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5038)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at android.view.View.layout(View.java:15671)
10-07 05:36:42.965 5827-5827/? E/AndroidRuntime:     at 

How can I use a Color as placeholder image with Picasso?

like image 456
stephan1002 Avatar asked Oct 07 '15 10:10

stephan1002


People also ask

How do you add a placeHolder in Picasso?

Just call . placeHolder() with a reference to a drawable (resource) and Picasso will display that as a placeholder until your actual image is ready. Picasso .

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.


3 Answers

You can understand from the error log itself android.content.res.Resources$NotFoundException: Resource ID #0xff141414

Use latest version of Picasso

And try this

Picasso.with(mContext).load("URL").placeholder(R.color.holder_color).error(R.color.error_color).into(viewHolder.imageView);
like image 134
Anoop M Maddasseri Avatar answered Oct 21 '22 17:10

Anoop M Maddasseri


In my project I used this solution to make a color placeholder

        gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setColor(color);

Picasso.with(context)
     .load(item.getImageUrls().get(0))
     .placeholder(gradientDrawable)
     .error(R.drawable.card_image)
    .centerCrop()
    .fit()
    .into(viewHolder.imageView);

This approach helps when your ImageView is set to wrap_content, as simple color fill will make your image invisible until the picture is loaded, because, as you know, color doesn't occupy any space.

like image 6
Kistamushken Avatar answered Oct 21 '22 17:10

Kistamushken


You can simply use class ColorDrawable. Also better to place color in file colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="your_color">#202020</color>
</resources>

And then your java code will look like:

Drawable placeholder = new ColorDrawable(getResources().getColor(R.color.your_color));

Picasso.with(context)
       .load(item.getImageUrls().get(0))
       .placeholder(placeholder)
       .error(R.drawable.card_image)
       .centerCrop()
       .fit()
       .into(viewHolder.imageView);

And if you're using Picasso in 2018 then replace with(context with get():

Drawable placeholder = new ColorDrawable(getResources().getColor(R.color.your_color));

Picasso.get()
       .load(item.getImageUrls().get(0))
       .placeholder(placeholder)
       .error(R.drawable.card_image)
       .centerCrop()
       .fit()
       .into(viewHolder.imageView);
like image 1
Alex Misiulia Avatar answered Oct 21 '22 15:10

Alex Misiulia