抛出OutOfMemoryError"无法分配31961100字节分配4194304空闲字节和27MB直到OOM

Vik*_*ngh 6 android

我正在从JSon将图像加载到图像视图中.JSon只带来图像URL的路径.我正在使用毕加索设定价值.但是它会给某些图像带来错误,而对于休息则它会正常工作.

Picasso.with(context).load(rowItem.getProductImages().get(0)).into(holder.productImageView);
Run Code Online (Sandbox Code Playgroud)

错误是:

 2771-2793/com.koove E/art? Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 4194304 free bytes and 27MB until OOM"
03-25 09:53:23.666    2771-2793/com.koove D/skia? --- decoder->decode returned false
Run Code Online (Sandbox Code Playgroud)

Lil*_*ilo 5

您应该使用 Picasso 中的 fit() 方法,它测量目标 ImageView 的尺寸,并在内部使用 resize() 将图像尺寸减小到 ImageView 的尺寸。

优点是图像具有尽可能低的分辨率,而不影响其质量。较低的分辨率意味着缓存中保存的数据较少。

Picasso.with(context).load(rowItem.getProductImages().get(0)).fit().into(holder.productImageView);
Run Code Online (Sandbox Code Playgroud)

如果仍然出现 OOM,请使用内存策略删除缓存。

Picasso.with(context).load(rowItem.getProductImages().get(0)).memoryPolicy(MemoryPolicy.NO_CACHE).fit().into(holder.productImageView);
Run Code Online (Sandbox Code Playgroud)