Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OutOfMemoryError Using Picasso Image

I am trying to load an image into an ImageView and am receiving an OutOfMemoryError. The image displays dynamically based on the specific view in the ViewPager. However, after I keep scrolling through the ViewPager, the image eventually fails to load and causes an OutOfMemoryError.

Note that I have 4 View in the ViewPager, and the app appears to crash on View 2. I am guessing the image is simply to large in size, but I was under the impression that Picasso handles accordingly:

Picasso Code:

Picasso.with(getActivity())
    .load(mImageURL)
    .fit()
    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_CACHE)
    .into((ImageView) rootView.findViewById(R.id.comments_image));

XML

<ImageView
    android:id="@+id/comments_image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".525"
    android:background="@color/black">
</ImageView>

Error

java.lang.OutOfMemoryError: Failed to allocate a 5074572 byte allocation with 132096 free bytes and 129KB until OOM
     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
     at android.graphics.Bitmap.nativeCreate(Native Method)
     at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
     at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
     at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
     at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:713)
     at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:280)
     at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:105)
     at android.widget.ImageView.onDraw(ImageView.java:1176)
     at android.view.View.draw(View.java:15231)
     at android.view.View.updateDisplayListIfDirty(View.java:14167)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.draw(View.java:15234)
     at android.view.View.updateDisplayListIfDirty(View.java:14167)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1131)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.draw(View.java:15234)
     at android.support.v4.view.ViewPager.draw(ViewPager.java:2341)
     at android.view.View.updateDisplayListIfDirty(View.java:14167)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1373)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.updateDisplayListIfDirty(View.java:14162)
     at android.view.View.getDisplayList(View.java:14189)
     at android.view.View.draw(View.java:14959)
     at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
     at android.view.View.draw(View.java:15234)
     at android.widget.FrameLayout.draw(F
like image 529
tccpg288 Avatar asked Nov 27 '25 13:11

tccpg288


1 Answers

I suggest you to use Glide, since it is designed to work well on low-cost (and worst hardware) devices by default.

A great difference between Picasso and Glide is that Glide's default format is RGB_565 when Picasso's ARGB_8888. As you can see below, it costs 50% less memory than Picasso to do the same job.

Memory cost

Still, if you aren't up to change your library, you can try resizing your image on Picasso's builder by using .resize(int, int) method.

like image 169
Lucas Albuquerque Avatar answered Nov 30 '25 01:11

Lucas Albuquerque