gpt4 book ai didi

将图像加载到recyclerview时Android picasso内存不足

转载 作者:行者123 更新时间:2023-11-30 01:22:29 25 4
gpt4 key购买 nike

我有一个 RecyclerView,它有很多项(100 多个)。每个项目都是一个 ImageView。在每个 ImageView 中,我使用 Picasso 库设置 Bitmap,如下所示:

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

我的问题是我在 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)

如果我捕捉到它,就会得到 OutOfMemoryError 并且稍后应用会崩溃。我能做些什么来防止这种情况发生?

最佳答案

尝试在 AndroidManefist.xmlApplication 标签中添加 android:largeHeap="true"

关于将图像加载到recyclerview时Android picasso内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com