1

まず、私の悪い英語をお詫びします。

製品カタログのアクティビティがある Android 用のアプリがあります。製品リストと写真からのデータは、別のルーチンでダウンロードされます。アクティビティの唯一の責任は、データ製品とそれぞれの写真をロードすることだけです。

データが 1 つのリストビューで作成されたことを示すために、BaseAdapter から継承するカスタム アダプター クラスがコードに従います。

いくつかの考慮事項。

  • ソース イメージの最大サイズは、高さまたは幅 500 ピクセルです。

  • 平均最大ファイル サイズは 45 kb です。

  • レコード数は 1200 ~ 2000 です

レイアウト/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>

レイアウト/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)

私はスタックオーバーフローからの次の議論で問題を解決しようとしましたが、うまくいきませんでした。

線形レイアウトの遅延読み込み (画像 + テキスト)

ListView へのレイジー イメージ ローダー

ListView での画像の遅延ロード

カスタム エンドレス ListView Android

ListView へのレイジー イメージ ローダー

https://github.com/thest1/LazyList

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

スケーリングとビットマップの読み込みで OOM (OutOfMemoryError) が発生する (Android)

4

2 に答える 2