gpt4 book ai didi

android - 在 BaseAdapter 中加载位图时如何修复错误 OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 21:28:59 24 4
gpt4 key购买 nike

首先,我为我糟糕的英语道歉。

我有一个适用于 Android 的应用程序,其中有一个产品目录 Activity 。产品列表和图片中的数据在单独的例程中下载。 Activity 的唯一职责就是加载数据产品及其各自的照片。

为了演示数据创建了一个 ListView 和一个继承自 BaseAdapter 的自定义适配器类,代码如下;

一些注意事项。

  • 源图像的最大尺寸为 500 像素的高度或宽度。

  • 平均最大文件大小为 45 kb。

  • 记录数在1200-2000之间

layout/catalogo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridView
android:id="@+id/gv_catalogo_gridprods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:verticalSpacing="5dp"
tools:listitem="@layout/catalogo_item" >
</GridView>

</LinearLayout>

layout/catalogo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fundo_imagem_catalogo"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:baselineAligned="false" >

<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_codigo"
android:textSize="12sp" />

<TextView
android:id="@+id/lb_catalogoitem_codigo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_000"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_descricao"
android:textSize="12sp" />

<TextView
android:id="@+id/lb_catalogoitem_descricao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/label_descricao"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

<ImageView
android:id="@+id/iv_catalogoitem_imagem"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:baselineAligned="false"
android:contentDescription="Imagem Produto"
android:src="@drawable/icone_android" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:baselineAligned="false" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_codbarras"
android:textSize="12sp" />

<TextView
android:id="@+id/lb_catalogoitem_codbarras"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_789000000"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_embalagem"
android:textSize="12sp" />

<TextView
android:id="@+id/lb_catalogoitem_emb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_cx012"
android:textStyle="bold" />

</LinearLayout>
</LinearLayout>

</LinearLayout>

适应 GridView

public class AdapterImagensCatalogo extends BaseAdapter {

Context contexto;
List<ItemCatalogo> catalogo;
BitmapFactory.Options optBitMap;

public AdapterImagensCatalogo(Context contexto, List<ItemCatalogo> catalogo) {
this.contexto = contexto;
this.catalogo = catalogo;
this.optBitMap = new BitmapFactory.Options();
this.optBitMap.inSampleSize = 10;

}

@Override
public int getCount() {
return catalogo.size();
}

@Override
public Object getItem(int position) {
return catalogo.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(contexto).inflate(R.layout.catalogo_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codigo);
tv.setText(catalogo.get(position).getProduto());

tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_descricao);
tv.setText(catalogo.get(position).getDescricao());

tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codbarras);
tv.setText(catalogo.get(position).getCodigoBarras());

tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_emb);
tv.setText(catalogo.get(position).getEmbalagem());

ImageView imgV = (ImageView) convertView.findViewById(R.id.iv_catalogoitem_imagem);
imgV.setImageBitmap(getMiniaturaImagem(catalogo.get(position).getPathArquivo()));

return convertView;
}


private Bitmap getMiniaturaImagem(String path){
return BitmapFactory.decodeFile(path, optBitMap);
}


}

问题发生在用户浏览图像列表时。尤其是当他如此快速地进行导航时。

这是 LogCat 错误。

    11-10 21:17:06.781: E/MessageQueue-JNI(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/MessageQueue-JNI(2526): at dalvik.system.NativeStart.main(Native Method)
11-10 21:17:06.781: D/AndroidRuntime(2526): Shutting down VM
11-10 21:17:06.781: W/dalvikvm(2526): threadid=1: thread exiting with uncaught exception (group=0x41a892a0)
11-10 21:17:06.781: E/AndroidRuntime(2526): FATAL EXCEPTION: main
11-10 21:17:06.781: E/AndroidRuntime(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/AndroidRuntime(2526): at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/AndroidRuntime(2526): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/AndroidRuntime(2526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/AndroidRuntime(2526): at dalvik.system.NativeStart.main(Native Method)

我已经尝试使用来自 stackoverflow 的以下讨论解决问题,但没有成功。

Lazy Loading (Image + Text) for Linear Layouts

Lazy Image Loader to ListView

Lazy load of images in ListView

Custom Endless ListView Android

Lazy Image Loader to ListView

https://github.com/thest1/LazyList

https://github.com/nostra13/Android-Universal-Image-Loader

Scaling and loading bitmap causes OOM (OutOfMemoryError) (Android)

最佳答案

首先,您没有在适配器中重新使用现有的 convertView。您应该重复使用此 View ,而不是每次都创建一个新 View :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(contexto).inflate(R.layout.catalogo_item, parent, false);
}
...
}

(注意:您在这里知道父项将是什么(ViewGroup parent 参数)所以您应该使用 inflate(id, parent, false) 告诉充气器这一点; 而不是 inflate(id, null); - 你几乎不应该调用 inflate(id, null))

这应该可以解决您的问题,因为您不再创建数百个 View ,而是回收现有 View 。如果没有,您可以在替换位图时明确回收位图,如下所示:

ImageView imgV = (ImageView) convertView.findViewById(R.id.iv_catalogoitem_imagem);
// Obtain a reference to the old image
Drawable oldImage = imgV.getDrawable();
// Set the new image
imgV.setImageBitmap(getMiniaturaImagem(catalogo.get(position).getPathArquivo()));
// Recycle the old image
if (oldImage != null) {
((BitmapDrawable)oldImage).getBitmap().recycle();
}

关于android - 在 BaseAdapter 中加载位图时如何修复错误 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895858/

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