2015-03-25 61 views
6

我从JSon加载图像到图像视图JSon只带来图像URL的路径我正在使用picasso设置值,但是它提供了错误的一些图像和其他人来说是工作的罚款投掷OutOfMemoryError“无法分配一个31961100字节分配4194304空闲字节和27MB直到OOM

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 
+1

您需要下载图像的背景,然后调整他们(或做一个副本),屏幕尺寸/图像大小(同样在背景中),然后显示出来。 – Tigger 2015-03-25 05:40:44

+2

[如何在Android中加载大图片并避免内存不足错误?](http://stackoverflow.com/questions/21392972/how-to-load-large-images-in-android-and-避免内存不足错误) – Tigger 2015-03-25 05:43:57

回答

4

您应该使用在毕加索的方法拟合(),它的测量目标的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); 
+0

很好的答案。 fit()真的适合我。只想知道我是否可以处理if(fit()== false)条件下的memoryPolicy()条件? – VVB 2016-02-15 09:46:00

+0

我不这么认为,fit()方法返回一个RequestCreator对象。由于使用fit()可以延迟图像请求,所以您可以使用resize()和onlyScaleDown()方法,然后仅在调整大小参数时调整图像大小。这里有一个很好的毕加索使用方法链接https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit – Lilo 2016-02-15 10:31:55

+0

好的。感谢您提供宝贵的意见。如果你能提出这个问题出现的原因,那将是很棒的事情。在一行中。 – VVB 2016-02-15 13:26:54

相关问题