我正在使用Kotlin和Picasso从URL显示图像。现在,我正在共享一些Intent,并且正在共享图像和文本。这是我的代码:
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?) { }
})
这有时会起作用。大多数时候,什么都没有发生。这是我的日志:
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
我已经读到目标必须放在一个字段中,但是我在与Kotlin共同努力。
看起来此代码在Activity
中,因此您只需向Activity
中添加一个字段并将其存储在其中:
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"))
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) { }
override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) { }
}
Picasso.get().load(URL).into(shareTarget)
}
// ...
}