2

我正在为我的应用程序制作一个简单的照片库。但是我遇到了一些非常烦人的内存泄漏,我不知道如何处理它们。

你们能帮帮我吗?我已经搜索了其他问题,但是根据答案,我似乎做对了。当然,我不是。

我的代码:

TouchImageView用来显示图像。

另外,我已经扩展ViewPager以支持缩放功能,如下所示:

public class ExtendedViewPager extends ViewPager {

    public ExtendedViewPager(Context context) {
        super(context);
    }

    public ExtendedViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v instanceof TouchImageView) {
            /*
            canScrollHorizontally is not supported for Api < 14. To get around this issue,
            ViewPager is extended and canScrollHorizontallyFroyo, a wrapper around
            canScrollHorizontally supporting Api >= 8, is called.
            */
            return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
        } else {
            return super.canScroll(v, checkV, dx, x, y);
        }
    }
}

其中一些照片可能很大(例如 2000x2000 像素)。所以我resample在将它们添加到视图之前,使用这个重采样函数:

public static Bitmap resample(int resourceID, boolean toFullScreen, Context c){
        //set the width and height we want to use as maximum display
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int targetWidth = display.getWidth();
        int targetHeight = display.getHeight();

        //create bitmap options to calculate and use sample size
        BitmapFactory.Options bmpOptions = new BitmapFactory.Options();

        //first decode image dimensions only - not the image bitmap itself
        bmpOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);

        //image width and height before sampling
        int currHeight = bmpOptions.outHeight;
        int currWidth = bmpOptions.outWidth;

        //variable to store new sample size
        int sampleSize = 1;

        //calculate the sample size if the existing size is larger than target size
        if (toFullScreen || currHeight > targetHeight || currWidth > targetWidth){
            //use either width or height
            if (toFullScreen || currWidth > currHeight)
                sampleSize = Math.round((float)currHeight / (float)targetHeight);
            else
                sampleSize = Math.round((float)currWidth / (float)targetWidth);
        }

        //use the new sample size
        bmpOptions.inSampleSize = sampleSize;
        //now decode the bitmap using sample options
        bmpOptions.inJustDecodeBounds = false;

        //get the file as a bitmap and return it
        Bitmap pic = BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);
        return pic;
    }

并在我的PagerAdapter实现中调用它,其中我还尝试recycle()使用位图:

private class TouchImagePagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return galleryImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = PhotoGalleryActivity.this;
        TouchImageView touchImageView = new TouchImageView(context);
        touchImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        touchImageView.setImageBitmap(Helper.resample(galleryImages[position].getImgResource(), false, getApplicationContext()));
        container.addView(touchImageView, 0);

        return touchImageView;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object object) {
        TouchImageView ti = (TouchImageView) object;
        BitmapDrawable bmpDrawable = (BitmapDrawable) ti.getDrawable();
        if (bmpDrawable != null) {
            Bitmap bmp = bmpDrawable.getBitmap();
            if(bmp != null && !bmp.isRecycled())
                bmp.recycle();
        }
        collection.removeView(ti);
        ti = null;
    }

    @Override
    public void destroyItem(View collection, int position, Object o) {
        View view = (View)o;
        ((ViewPager) collection).removeView(view);
        view = null;
    }
}

有什么线索吗?提前致谢。

4

2 回答 2

1

如果原始 bmp 大小大于 imageview,我建议将 inSampleSize 更改为 2 或 4。你说有时图像大于 2000x2000。这可能会导致内存问题。

于 2015-05-18T02:09:27.347 回答
1

在标记的项目的清单文件中添加以下行

机器人:大堆=“真”

对于 EX:

   <application
    android:name="com......"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/CustomActionBarTheme">

这可以帮助您为应用程序工作提供更多内存

于 2015-05-18T05:00:13.483 回答