Android 抛出OfMemoryError“;无法分配31961100字节的分配,其中4194304个可用字节和27MB的空间直到OOM

Android 抛出OfMemoryError“;无法分配31961100字节的分配,其中4194304个可用字节和27MB的空间直到OOM,android,Android,我正在从JSon加载图像到图像视图。JSon只带来图像URL的路径。我正在用毕加索设定值。但对于某些图像,它会产生错误,对于其他图像,它工作正常 Picasso.with(context).load(rowItem.getProductImages().get(0)).into(holder.productImageView); 错误是: 2771-2793/com.koove E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 31

我正在从JSon加载图像到图像视图。JSon只带来图像URL的路径。我正在用毕加索设定值。但对于某些图像,它会产生错误,对于其他图像,它工作正常

Picasso.with(context).load(rowItem.getProductImages().get(0)).into(holder.productImageView);
错误是:

 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

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

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

Picasso.with(context).load(rowItem.getProductImages().get(0)).fit().into(holder.productImageView);
如果仍然有OOM,请使用内存策略删除缓存

Picasso.with(context).load(rowItem.getProductImages().get(0)).memoryPolicy(MemoryPolicy.NO_CACHE).fit().into(holder.productImageView);

您需要在后台下载图像,然后将其调整大小(或制作一个副本)至屏幕大小/演示文稿大小(再次在后台),然后显示它们。可能会重复伟大的答案。fit()对我来说真的很有用。只是想知道我可以在if(fit()==false)条件下处理memoryPolicy()条件吗?我不这么认为,fit()方法返回一个RequestCreator对象。由于使用fit()可以延迟图像请求,因此可以改用resize()和onlyScaleDown()方法,然后,仅当图像的大小大于resize参数时,才会调整图像的大小。这里有一个关于毕加索使用方法的链接。感谢您添加有价值的输入。如果你能提到这个问题产生的原因,那就太好了?排成一行。