0

这真的很令人沮丧——尽管我回收了位图并使用了垃圾收集器,但我的应用程序中出现了某种内存泄漏。在我的应用程序中,我有一个列表视图,当单击子项时,几个图像会加载到alerdialogbuilder 和滚动视图中。我在滚动视图中没有超过 9 张图像,过了一会儿我得到了内存不足的异常。除了回收位图,我还能做什么?例如,我是否应该创建图像视图的弱引用,以便垃圾收集器可以处理它们?

我将所有内容放在扩展 Asynctask 的类中,并在 do-in-background-method 中调用另一个方法,以便有效地缩小图像。然后将图像发送到 onPostExecute(Bitmap[] bitmap) 方法,然后将它们放入滚动视图中。泄漏点在哪里?我的 Galaxy s3 的堆大小为 64 mb,仅加载 9 张图像后,内存使用量飙升至 42 mb,然后再单击至 47 mb ......然后什么都没有发生奇怪......我想,好吧.. 很好,它停在这里在 47 mb 上......但在再点击 10 次后,内存使用量继续攀升至 55 mb .. 依此类推,直到应用程序崩溃。

我通过在清单文件中设置 large-heap = true 暂时解决了它。但我不喜欢推迟问题!

我也无法理解为什么 9 张图像的内存使用量如此之大。在缩小之前,图像的平均大小约为 250kb。

非常感谢帮助!!!:-)

    class BitmapWorkerTask extends AsyncTask <Integer, Void, Bitmap[]> {

        private int[] data;
        private int[] width, height;
        private int nmbrOfImages;
        private String[] scrollText;
        private ImageView mImage;
        private View view;
        private LayoutInflater factory;
        private AlertDialog.Builder alertadd;

        public BitmapWorkerTask(int[] width, int[] height, int nmbrOfImages, String[] scrollText) {

            this.width = width;
            this.height = height;
            this.nmbrOfImages = nmbrOfImages;
            this.scrollText = scrollText;

            mImage = null;
            view = null;
            factory = null;
            alertadd = null;
            System.gc();

            try {
                for (int i = 0; i < scaledBitmap.length; i++) {
                    scaledBitmap[i].recycle();
                    System.out.println("i: " + i);
                }
            } catch (NullPointerException ne) {
                System.out.println("nullpointerexception ... gick inte recycla bitmapbilder");
            }


            switch (nmbrOfImages) {
            case 0:
                data = new int[1];

                break;
            case 1:
                data = new int[3];

                break;
            case 2:
                data = new int[5];

                break;
            case 3:
                data = new int[9];

                break;
            }


        }

        @Override
        protected Bitmap[] doInBackground(Integer ... params) {

            switch (nmbrOfImages) {
            case 0:
                data[0] = params[0];
                break;
            case 1:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                break;
            case 2:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                data[3] = params[3];
                data[4] = params[4];
                break;
            case 3:
                data[0] = params[0];
                data[1] = params[1];
                data[2] = params[2];
                data[3] = params[3];
                data[4] = params[4];
                data[5] = params[5];
                data[6] = params[6];
                data[7] = params[7];
                data[8] = params[8];
                break;
            }

            alertadd = new AlertDialog.Builder(context);
            factory = LayoutInflater.from(context);

            return decodeSampledBitmapFromResource(context.getResources(), data, width, height);
        }

        protected void onPostExecute(Bitmap[] bitmap) { 
            switch (nmbrOfImages) {
                case 0:
                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews, null);
                    }
                    ImageView mImage = (ImageView) view.findViewById(R.id.extra_img);
                    mImage.setImageBitmap(bitmap[0]);
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();
                break;
                case 1:

                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews2, null);
                    }

                    mImage = (ImageView) view.findViewById(R.id.img1);
                    mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);

                    try {
                        TextView mText2 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText2.setText(scrollText[0]);
                        mText2 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText2.setText(scrollText[1]);
                        mText2 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText2.setText(scrollText[2]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();

                break;
                case 2:

                    if (view == null) { 
                        view = factory.inflate(R.layout.alertviews3, null);
                    }
                    mImage = (ImageView) view.findViewById(R.id.img1);
                    mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);
                    mImage = (ImageView) view.findViewById(R.id.img4);
                    mImage.setImageBitmap(bitmap[3]);
                    mImage = (ImageView) view.findViewById(R.id.img5);
                    mImage.setImageBitmap(bitmap[4]);

                    try {
                        TextView mText3 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText3.setText(scrollText[0]);
                        mText3 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText3.setText(scrollText[1]);
                        mText3 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText3.setText(scrollText[2]);
                        mText3 = (TextView) view.findViewById(R.id.text_img4_scrollview);
                        mText3.setText(scrollText[3]);
                        mText3 = (TextView) view.findViewById(R.id.text_img5_scrollview);
                        mText3.setText(scrollText[4]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }
                    alertadd.setView(view);
                    alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dlg, int sumthin) {

                        }
                    });
                    alertadd.show();

                break;
                case 3:

                if (view == null) { 
                    view = factory.inflate(R.layout.alertviews4, null);
                }

                AlertDialog.Builder alertadd = new AlertDialog.Builder(context);

                mImage = (ImageView) view.findViewById(R.id.img1);
                mImage.setImageBitmap(bitmap[0]);
                    mImage = (ImageView) view.findViewById(R.id.img2);
                    mImage.setImageBitmap(bitmap[1]);
                    mImage = (ImageView) view.findViewById(R.id.img3);
                    mImage.setImageBitmap(bitmap[2]);
                    mImage = (ImageView) view.findViewById(R.id.img4);
                    mImage.setImageBitmap(bitmap[3]);
                    mImage = (ImageView) view.findViewById(R.id.img5);
                    mImage.setImageBitmap(bitmap[4]);
                    mImage = (ImageView) view.findViewById(R.id.img6);
                    mImage.setImageBitmap(bitmap[5]);
                    mImage = (ImageView) view.findViewById(R.id.img7);
                    mImage.setImageBitmap(bitmap[6]);
                    mImage = (ImageView) view.findViewById(R.id.img8);
                    mImage.setImageBitmap(bitmap[7]);
                    mImage = (ImageView) view.findViewById(R.id.img9);
                    mImage.setImageBitmap(bitmap[8]);

                    try {
                        TextView mText4 = (TextView) view.findViewById(R.id.text_img1_scrollview);
                        mText4.setText(scrollText[0]);
                        mText4 = (TextView) view.findViewById(R.id.text_img2_scrollview);
                        mText4.setText(scrollText[1]);
                        mText4 = (TextView) view.findViewById(R.id.text_img3_scrollview);
                        mText4.setText(scrollText[2]);
                        mText4 = (TextView) view.findViewById(R.id.text_img4_scrollview);
                        mText4.setText(scrollText[3]);
                        mText4 = (TextView) view.findViewById(R.id.text_img5_scrollview);
                        mText4.setText(scrollText[4]);
                        mText4 = (TextView) view.findViewById(R.id.text_img6_scrollview);
                        mText4.setText(scrollText[5]);
                        mText4 = (TextView) view.findViewById(R.id.text_img7_scrollview);
                        mText4.setText(scrollText[6]);
                        mText4 = (TextView) view.findViewById(R.id.text_img8_scrollview);
                        mText4.setText(scrollText[7]);
                        mText4 = (TextView) view.findViewById(R.id.text_img9_scrollview);
                        mText4.setText(scrollText[8]);
                    } catch (NullPointerException ne) {
                        System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
                    }

                alertadd.setView(view);

                alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dlg, int sumthin) {

                    }
                });
                alertadd.show();

            break;
            }
        }



    }
4

0 回答 0