问题
I am trying to show bitmap images in a 3 column gridViewLayout in recyclerView. but I receive OOM (Out of Memory) exception.
Note that images are picked from gallery
how can I solve this problem ??
This is my onBindViewHolder :
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final byte[] data = arrayList.get(position).getAsByteArray("byteArray");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Glide.with(activity)
.load(bitmap)
.apply(new RequestOptions().override(200, 200))
.into(holder.image);
}
and This is the error part of my logCat :
java.lang.OutOfMemoryError: Failed to allocate a 7680012 byte allocation with 4194208 free bytes and 6MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:564)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:587)
at com.apd.atxhider.allGalleryAdapter.onBindViewHolder(allGalleryAdapter.java:89)
at com.apd.atxhider.allGalleryAdapter.onBindViewHolder(allGalleryAdapter.java:53)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2483)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2180)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
Thanks for your help!
回答1:
If Bitmap data is large, decodeByteArray will be caused Out of memory error. So you need decode bytearray with Glide directly.
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final byte[] data = arrayList.get(position).getAsByteArray("byteArray");
Glide.with(activity)
.load(data)
.apply(new RequestOptions().override(200, 200))
.into(holder.image);
}
来源:https://stackoverflow.com/questions/65724496/how-to-avoid-the-error-oom-when-displaying-bitmaps-in-recyclerview