由于调用bitmapfactory而导致内存泄漏

时间:2013-06-19 10:58:35

标签: android memory-leaks out-of-memory bitmapfactory

我昨天写了关于记忆泄漏和记忆不足的文章。

看我的帖子:

memory-leak and out-of-memory

我有一些使用内存分析器的好技巧,我开始使用它,我可能找到了一些但我无法解决它。在DDMS中,可以选择跟踪对象的分配。我在点击子列表项目时发现了一些有趣的东西(将9个图像加载到滚动视图中)。 )图像应分配9次 - 但我可以看到它分配了27次。由于Bitmap类的createScaledBitmap方法分配了9次,然后由于BitmapFactory类的调用而分配了18次。 WHY ???

我将发布下面缩放位图的方法。代码能写得更好吗? 9个图像的资源分配27次是逻辑吗?我是否多次调用BitmapFactory.decodeResource?但我只能回收一次。

下面的导入和方法是 decodeSampledBitmapFromResource

很有帮助!!!

/**
       * 
       * @param options
       * @param reqW
       * @param reqH
       * @return
       */
      public int calculateInSampleSize(BitmapFactory.Options options, int reqW, int reqH) {

            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            int inSampleSize = 1;
            if (imageHeight > reqH || imageWidth > reqW) {
                int heightRatio = Math.round((float) imageHeight / (float) reqH);
                int widthRatio = Math.round((float) imageWidth / (float) reqW);
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
                System.out.println("i if-satsen!");
                System.out.println("height-ratio: " + heightRatio + "\nwidth-ratio: " + widthRatio);
            }
            System.out.println("samplesize: " +  inSampleSize);
            inSampleSize = inSampleSize;

            return inSampleSize;
        }

        @SuppressLint("NewApi")
        /**
         * 
         * @param res
         * @param resId
         * @param reqW
         * @param reqH
         * @return
         */
        public Bitmap[] decodeSampledBitmapFromResource(Resources res, int[] resId, int[] reqW, int[] reqH) {

            this.scaledBitmap = new Bitmap[resId.length];
            BitmapFactory.Options options;
            for (int i = 0; i < resId.length; i++) {
                options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(res, resId[i], options);

                System.out.println("ursprunglig bild: h = " + options.outHeight + " w = " + options.outWidth);
                options.inSampleSize = calculateInSampleSize(options, reqW[i], reqH[i]);

                while (options.outHeight < reqH[i] || options.outWidth < reqW[i]) {

                    options.inSampleSize--;
                    System.out.println("räknar nu ner insampleseize\ninSamleSize =" + options.inSampleSize);
                }

                options.inJustDecodeBounds = false;

                Bitmap bm = BitmapFactory.decodeResource(res, resId[i], options);
                System.out.println("innan omskalning: h = " + options.outHeight + " w = " + options.outWidth);
                System.out.println("antalet bytes: " + bm.getByteCount());
                System.out.println("native free size: " + Debug.getNativeHeapFreeSize() );

                this.scaledBitmap[i] = Bitmap.createScaledBitmap(bm, reqW[i], reqH[i], true); 
                bm.recycle();

            }
            System.gc();
            return this.scaledBitmap;
        }

0 个答案:

没有答案