【问题标题】:Picasso garbage collecting Target毕加索垃圾收集目标
【发布时间】:2020-08-31 11:30:01
【问题描述】:

我正在使用 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 来实现这一点。

【问题讨论】:

    标签: android kotlin garbage-collection picasso


    【解决方案1】:

    看起来这段代码在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)
        }
        // ...
    }
    

    请注意,这假设您一次只处理其中一个请求。如果您有多个,则必须将它们存储在某种集合或多个变量或类似的集合中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2012-05-04
      相关资源
      最近更新 更多