毕加索垃圾收集目标
Picasso garbage collecting Target
我正在使用 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"))
// 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)
}
// ...
}
请注意,这假设您一次只有其中一个请求正在进行中。如果有多个,则必须将它们存储在某种集合或多个变量或类似的东西中。
我正在使用 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"))
// 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)
}
// ...
}
请注意,这假设您一次只有其中一个请求正在进行中。如果有多个,则必须将它们存储在某种集合或多个变量或类似的东西中。