在recyclerView中显示位图时如何避免错误OOM

how to avoid the error OOM when displaying bitmaps in recyclerView

我正在尝试在 recyclerView 的 3 列 gridViewLayout 中显示位图图像。但我收到 OOM(内存不足)异常。

请注意,图片是从图库中挑选的

我该如何解决这个问题??

这是我的 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);
}

这是我的 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)

感谢您的帮助!

如果Bitmap数据较大,decodeByteArray会导致Out of memory错误。 所以你需要直接用Glide decode bytearray

@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);
}