I have a RecyclerView
, which have many items (100+). Each item is an ImageView
. In each ImageView
I set Bitmap
using Picasso
library like this:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String photoUrl = photos.get(position).getPath();
File photoFile = new File(photoUrl);
Picasso.with(context).load(photoFile).fit().centerCrop().into(holder.photo);
}
My problem is that I catch stacktrace in logcat:
04-27 14:48:43.613 27706-27740/com.rcd.perfecto E/dalvikvm-heap: Out of memory on a 30720016-byte allocation.
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: "Picasso-/storage/emulated/0/DCIM/Camera/IMG_20150921_100202_HDR.jpg" prio=5 tid=25 RUNNABLE
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: | group="main" sCount=0 dsCount=0 obj=0x42badb28 self=0x560cd3c0
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: | sysTid=27740 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1443672328
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: | state=R schedstat=( 818421079 614394933 1660 ) utm=75 stm=6 core=0
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:627)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at com.squareup.picasso.BitmapHunter.decodeStream(BitmapHunter.java:142)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:217)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at java.lang.Thread.run(Thread.java:841)
04-27 14:48:43.613 27706-27740/com.rcd.perfecto I/dalvikvm: at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)
And if I've catch that, then I get OutOfMemoryError
and apllication crashes later. What can I do to prevent this?