1

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?

Oleg Ryabtsev
  • 457
  • 2
  • 9
  • 24

4 Answers4

1

Try to add android:largeHeap="true" in you Application tag in AndroidManefist.xml

Ahmed M. Abed
  • 599
  • 3
  • 9
  • 22
1

It is better to either use .fit() or .resize(width, height).

1) .fit() - This will result in delayed execution of the request until the ImageView has been laid out. Note: This method works only when your target is an ImageView.

2) .resize(width, height) - This will resize the image to the specified size in pixels.

Refer : http://square.github.io/picasso/2.x/picasso/

Sachin K Pissay
  • 683
  • 1
  • 11
  • 21
0

try to play with picasso Cache policy https://futurestud.io/blog/picasso-influencing-image-caching

or use UniversalImageLoader instead of Picasso ;-)

wojciech_maciejewski
  • 1,277
  • 1
  • 12
  • 28
0

See you are trying to load large file size into imageview using Picassso. Picasso will through memory exception if the file size you are loading is larger than 1.5MB.

So inorder to load large file using picasso, you have to resize it and load it into imageview. Try this.

Picasso.with(context)
    .load(photoFile)
    .transform(new BitmapTransform(300,300))
    .skipMemoryCache()
    .resize(200,200)
    .centerInside()
    .into(holder.photo);
HourGlass
  • 1,805
  • 1
  • 15
  • 29