gpt4 book ai didi

java - 使用 RecyclerView 和 Picasso 问题处理大量图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:57 26 4
gpt4 key购买 nike

更新
我发现了我的问题。我正在尝试使用 RecyclerView 处理大量图像(.webp 格式的 770 个图像,图像总大小为 10mb),但它显示内存不足错误。
如何解决这个问题?
我正在使用 picasso 加载图像


抛出 OutOfMemoryError“无法分配具有 107504 个可用字节和 104KB 的 810076 字节分配,直到 OOM”


如果列表大小小于 100 没有问题,如何使用回收器 View 加载 700 项。每个项目都包含 webp 格式的图像
我正在尝试这段代码,它抛出内存不足错误并使应用程序崩溃。如何解决这个错误?
(它包含 700 个图像,全部为 webp 格式,此代码位于具有网格布局的 recyclerview 中)

 InputStream ins = null;

try {
ins = mContext.getAssets().open(mData.get(position).getImagename()+".webp");
Bitmap bitmap = BitmapFactory.decodeStream(ins);
holder.image.setImageBitmap(bitmap);
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (ins != null)
try {
ins.close();
} catch (IOException e) { }
}

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:805) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:845) at tropicalfishhandbook.midhilaj.com.v2.a.a.a(Unknown Source) at tropicalfishhandbook.midhilaj.com.v2.a.a.onBindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(Unknown Source) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(Unknown Source) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(Unknown Source) at android.support.v7.widget.GridLayoutManager.layoutChunk(Unknown Source) at android.support.v7.widget.LinearLayoutManager.fill(Unknown Source) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(Unknown Source) at android.support.v7.widget.GridLayoutManager.onLayoutChildren(Unknown Source) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(Unknown Source) at android.support.v7.widget.RecyclerView.onMeasure(Unknown Source) at android.view.View.measure(View.java:16749) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413) at android.widget.LinearLayout.measureVertical(LinearLayout.java:696) at android.widget.LinearLayout.onMeasure(LinearLayout.java:589) at android.view.View.measure(View.java:16749) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1235) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.widget.ScrollView.onMeasure(ScrollView.java:327) at android.view.View.measure(View.java:16749) at android.support.constraint.ConstraintLayout.a(Unknown Source) at android.support.constraint.ConstraintLayout.onMeasure(Unknown Source) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.support.design.widget.CoordinatorLayout.a(Unknown Source) at android.support.design.widget.h.a(Unknown Source) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.a(Unknown Source) at android.support.design.widget.CoordinatorLayout.onMeasure(Unknown Source) at android.view.View.measure(View.java:16749) at android.support.v4.widget.DrawerLayout.onMeasure(Unknown Source) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.support.v7.widget.ContentFrameLayout.onMeasure(Unknown Source) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413) at android.widget.LinearLayout.measureVertical(LinearLayout.java:696) at android.widget.LinearLayout.onMeasure(LinearLayout.java:589) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16749) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413) at android.widget.LinearLayout.measureVertical(Lin

我的 list 文件

<application
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
tools:replace="android:allowBackup"
android:theme="@style/AppTheme">

更新我用 Picasso 替换,它在 2GB RAM 的智能手机中工作,在 500MB 的智能手机中崩溃,有时在 1GB RAM 的智能手机中也崩溃
添加 picasso 后,现在图像加载需要 5 秒

更新了新代码 但问题没有解决


这是我用来加载的代码

try {
List<Millafish_profile> list = mDatabaseManager.getAll();
for (int i = 0; i < list.size(); i++) {
final int finalI = i;//"@drawable/" +
String uri = list.get(i).getPic().replace("’", "_").replace(".jpg", "").replace(".png", "").replace("-", "_"); // where myresource (without the extension) is the file
// Log.i("uri_"+i,uri+"");
Millafish_profile millafish_profile = list.get(i);//mDatabaseManager.getprofile(mDatabaseManager.getallfishname().get(finalI));
millafish_profile.setImageuri(uri.toLowerCase());
millafish_profileList.add(millafish_profile);
//if(i==20)break;

{
//adapter.notifyItemChanged(i);
//(millafish_profileList.size());
}

}
adapter.notifyDataSetChanged();
millafish_profileList_org = new ArrayList<>();
millafish_profileList_org.addAll(millafish_profileList);

}catch (Exception e){
Log.e("homepage",e.getMessage()+"");
}

最佳答案

删除 Picasso 并使用 Glide

Glide.with(mContext).load(Uri.parse("file:///android_asset/"+mData.get(position).getImageuri()+".webp"))
.thumbnail(0.5f)

.into(holder.image);

关于java - 使用 RecyclerView 和 Picasso 问题处理大量图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834396/

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