I'm using Kotlin and Picasso to display images from URL. Now I am sharing some content with an Intent, and I am sharing the image and some text. Here is my code:
Picasso.get().load(URL).into(object : Target {
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND_MULTIPLE
putExtra(Intent.EXTRA_STREAM, getBitmapFromView(bitmap, this@DealActivity))
putExtra(Intent.EXTRA_TEXT, textForShare)
type = "image/jpeg"
}
startActivity(Intent.createChooser(shareIntent, "SEND"))
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) { }
override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) { }
})
This works, some of the time. Most of the time, nothing happens. Here is my log:
2020-05-14 22:12:52.378 11310-11402/il.co.nazooza D/Picasso: Dispatcher enqueued [R19]+2ms
2020-05-14 22:12:52.380 11310-11430/il.co.nazooza D/Picasso: Hunter executing [R19]+3ms
2020-05-14 22:12:53.150 11310-11430/il.co.nazooza D/Picasso: Hunter decoded [R19]+773ms
2020-05-14 22:12:53.151 11310-11402/il.co.nazooza D/Picasso: Dispatcher batched [R19]+775ms for completion
2020-05-14 22:12:53.195 11310-11310/il.co.nazooza D/Picasso: Main canceled [R19]+813ms target got garbage collected
I have read that the target must be made into a field, but I'm struggling to to this with Kotlin.
It looks like this code is in an Activity
, so you just need to add a field to your Activity
and store it there:
class MyActivity: Activity() {
private var shareTarget: Target? = null
//...
fun someMethod() {
shareTarget = object : Target {
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND_MULTIPLE
putExtra(Intent.EXTRA_STREAM, getBitmapFromView(bitmap, this@DealActivity))
putExtra(Intent.EXTRA_TEXT, textForShare)
type = "image/jpeg"
}
startActivity(Intent.createChooser(shareIntent, "SEND"))
// don't need to store it any longer
shareTarget = null
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) { }
override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
// don't need to store it any longer
shareTarget = null
}
}
Picasso.get().load(URL).into(shareTarget)
}
// ...
}
Note that this assumes you only have one of these requests in-flight at a time. If you have multiple, you'll have to store them in a collection of some sort, or multiple variables, or something like that.