Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso Callback with Kotlin

I'm making an Android app with Kotlin, and need to use Picasso to download images. I saw this Java code below for setting animations to images, but I can't convert it to Kotlin, cause I don't know how to set Callback in "into" function.

Picasso.with(MainActivity.this)
       .load(imageUrl)
       .into(imageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        //set animations here

                    }

                    @Override
                    public void onError() {
                        //do smth when there is picture loading error
                    }
                });

Can someone help me ?

My actual code :

Picasso.with(context)
       .load(url)
       .into(imageDiapo, com.squareup.picasso.Callback)
like image 487
Valentin Garcia Avatar asked Apr 24 '19 12:04

Valentin Garcia


People also ask

How will you load an image into an imageView from an image URL using Picasso and display custom image if there is an error while loading the image?

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

Picasso.with(MainActivity::this)
       .load(imageUrl)
       .into(imageView, object: com.squareup.picasso.Callback {
                    override fun onSuccess() {
                        //set animations here

                    }

                    override fun onError(e: java.lang.Exception?) {
                        //do smth when there is picture loading error
                    }
                })
like image 71
Saeed Entezari Avatar answered Oct 19 '22 05:10

Saeed Entezari


In Latest version onError recives an Exception as parameter and uses get() instead of with() :

  Picasso.get()
  .load(imageUrl)
  .into(imageView, object :Callback{
   override fun onSuccess() {
   Log.d(TAG, "success")
   }

   override fun onError(e: Exception?) {
   Log.d(TAG, "error")
   }
   })

and On the previous version

   Picasso.with(MainActivity::this)
   .load(imageUrl)
   .into(imageView, object: Callback {
                override fun onSuccess() {
                   Log.d(TAG, "success")
                }

                override fun onError() {
                  Log.d(TAG, "error")
                }
            })
like image 27
Kapil Rajput Avatar answered Oct 19 '22 06:10

Kapil Rajput


Hi here are some different ways that Picasso provides:

Picasso.with(context).load(path).into(imageView);

2.create a new file inside our utils package, call it picasso.kt and fill it with simple code below:

 public val Context.picasso: Picasso
    get() = Picasso.with(this)

3. While this corresponds to the receiver object we can invoke following code on any Context:

picasso.load(path).into(imageView)
  1. We can go further and extend ImageView class like:

    public fun ImageView.load(path: String, request: (RequestCreator) -> RequestCreator) {
    request(getContext().picasso.load(path)).into(this)    }
    
like image 1
Varun Malhotra Avatar answered Oct 19 '22 06:10

Varun Malhotra