gpt4 book ai didi

android - picasso 图书馆 - 内存不足

转载 作者:可可西里 更新时间:2023-11-01 18:55:59 27 4
gpt4 key购买 nike

我在我的应用程序中使用 Picasso 库最新版本 2.4.0 来下载和缓存图像。大约有 25-30 张图像,每张大小为 300KB-400KB。我认为这绝不是一件大事或一件沉重的事情。

尽管应用程序运行良好,但我的 logcat 中的内存分配不足。谁能解释为什么会这样?

在 GridView 适配器中加载图像的代码:

Picasso.with(mContext).load(getUrl()).placeholder(R.drawable.placeholder)
.into(viewholder.image);

这是我的 Logcat 输出:

I/dalvikvm-heap(11142): Grow heap (frag case) to 53.860MB for 2720016-byte allocation
I/dalvikvm-heap(11142): Forcing collection of SoftReferences for 3265936-byte allocation
E/dalvikvm-heap(11142): Out of memory on a 3265936-byte allocation.
I/dalvikvm(11142): "Picasso-/images/posters/34.71.jpg" prio=5 tid=18 RUNNABLE
I/dalvikvm(11142): | group="main" sCount=0 dsCount=0 obj=0x4283f248 self=0x60a47830
I/dalvikvm(11142): | sysTid=11196 nice=10 sched=0/0 cgrp=apps/bg_non_interactive I/dalvikvm(11142): | state=R schedstat=( 2070202497 1858185620 3947 ) utm=172 stm=35 core=3
I/dalvikvm(11142): at android.graphics.Bitmap.nativeCreate(Native Method)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:168)
I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:111)
I/dalvikvm(11142): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390I/dalvikvm(11142): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.I/dalvikvm(11142): at java.lang.Thread.run(Thread.java:841)
I/dalvikvm(11142): at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:408)
I/dalvikvm-heap(11142): Forcing collection of SoftReferences for 3265936-byte allocation
E/dalvikvm-heap(11142): Out of memory on a 3265936-byte allocation.
I/dalvikvm(11142): "Picasso-/images/posters/34.71.jpg" prio=5 tid=17 RUNNABLE
I/dalvikvm(11142): | group="main" sCount=0 dsCount=0 obj=0x42841b88 self=0x5ec91f90
I/dalvikvm(11142): | sysTid=11183 nice=10 sched=0/0 cgrp=apps/bg_non_interactive I/dalvikvm(11142): | state=R schedstat=( 2050467088 1713164574 3713 ) utm=172 stm=32 core=3
I/dalvikvm(11142): at android.graphics.Bitmap.nativeCreate(Native Method)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:168)
I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:111)
I/dalvikvm(11142): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390I/dalvikvm(11142): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.I/dalvikvm(11142): at java.lang.Thread.run(Thread.java:841)
I/dalvikvm(11142): at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:408)

最佳答案

您的原始代码是下载完整图像,将完整图像加载到内存中,然后让 Android 缩小图像以适合您的 ImageView

在这种情况下,您不需要内存中的完整图像——您需要尺寸接近 ImageView 的尺寸。

Picasso 上的

fit() 处理这个问题。它使用 BitmapFactory.Options 上的 inSampleSize 在图像加载到内存中时对图像进行缩减采样,让您获得与 ImageView 大小相近的东西,让 Android 从那里扩展。这将显着减少每个图像的内存占用,特别是取决于您使用的 ImageView 有多大。

关于android - picasso 图书馆 - 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26938310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com