I load images from urls using Picasso. Image loading worked so far for every image until I tried it with an jpeg image that has higher size than others (1MB). I try to load the image using imageTarget that should not be garbage collected as it is declared as below:
fun loadImage(view: ImageView, displayImageUrl: String?) {
val imageTarget: com.squareup.picasso.Target = object : com.squareup.picasso.Target {
override fun onBitmapLoaded(bitmap: Bitmap, loadedFrom: Picasso.LoadedFrom) {
view.setImageBitmap(bitmap)
}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?)
}
override fun onPrepareLoad(drawable: Drawable?) {
}
}
}
onBitmapLoaded
is not executed, only onPrepareLoad
is.
Picasso logs below say that target was garbage collected, but how is this possible if onPrepareLoad
is executed. Does this message refer to the downloaded image that was cleaned from memory? The image is loaded using Picasso earlier for a list view that contains a thumbnail of the image and works fine as I can see the image on the list, but when I open the screen with the large view of the image it does not load it.
2022-03-25 10:03:41.462 8949-9021/mobile.debug D/Picasso: Dispatcher enqueued [R5] 2ms
2022-03-25 10:03:41.463 8949-9023/mobile.debug D/Picasso: Hunter executing [R5] 3ms
2022-03-25 10:03:42.386 8949-9023/mobile.debug D/Picasso: Hunter decoded [R5] 925ms
2022-03-25 10:03:42.388 8949-9021/mobile.debug D/Picasso: Dispatcher batched [R5] 927ms for completion
2022-03-25 10:03:42.505 8949-8963/mobile.debug I/ge.mobile.debu: NativeAlloc concurrent copying GC freed 80414(6068KB) AllocSpace objects, 75(1628KB) LOS objects, 49% free, 14MB/28MB, paused 823us total 133.822ms
2022-03-25 10:03:42.523 8949-8949/mobile.debug D/Picasso: Main canceled [R5] 1062ms target got garbage collected
2022-03-25 10:03:42.631 8949-9021/mobile.debug D/Picasso: Dispatcher delivered [R5] 1171ms
2022-03-25 10:03:42.631 8949-8949/mobile.debug D/Picasso: Main completed [R5] 1171ms from DISK
The call for loading the image is simple Picasso.get().load(displayImageUrl).into(imageTarget)
CodePudding user response:
You should use resize to load exactly size:
Picasso.with(this).load(imageUrl).resize(width, height).into(imageView);
CodePudding user response:
Picasso does load images greater than 1MB.
Picasso.get().load(url)
.placeholder(R.drawable.gradient_placeholder_horizontal)
.into(image)
Don't use Target. It doesn't work properly sometimes. Especially in a recycler view.
To get Bitmap use. you can get size with it.
Bitmap bitmap = Picasso.get().load(url).get();
you can use this resize & onlyScaleDown(). Only scaledown will only resize if it is greater than given value in resize
Picasso.get().load(url)
.placeholder(R.drawable.gradient_placeholder_horizontal)
.resize(476, 678)
.onlyScaleDown()
.centerCrop()
.into(image)