使用毕加索的内存不足异常

Out of memory Exception using Picasso

我需要在视图寻呼机中显示图像。要在图像视图中显示图像,我使用 Picasso 库。我还使用屏幕大小调整图像大小。
所以在 OnCreate 的 Activity 中,我得到了 view pager:

mPager=(ViewPager)findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(); mPager.setAdapter(mPagerAdapter);

然后获取设备尺寸:

Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenWidth = size.x;
    screenHeight = size.y;

这是我的页面适配器:

 private class ScreenSlidePagerAdapter extends  PagerAdapter {
    PhotoViewAttacher attacher;

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

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

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

    @Override
    public Object instantiateItem(View context, int position) {

        LayoutInflater ltInflater = getLayoutInflater();
        View view = ltInflater.inflate(R.layout.page_fragment, null, false);
        final ImageView imageView = (ImageView)view.findViewById(R.id.image);
        attacher = new PhotoViewAttacher(imageView);
        attacher.setZoomable(true);

        Picasso.with(MainActivity.this).setIndicatorsEnabled(true);
        Picasso.with(MainActivity.this)
                .load(Constants.imageUrls.get(position))
                .resize(screenWidth, screenHeight).centerInside()
                .into(imageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        if (attacher != null) {
                            attacher.update();
                        } else {
                            attacher = new PhotoViewAttacher(imageView);
                        }
                    }

                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub
                        int i = 0;
                    }
                });

                ((ViewPager) context).addView(view);
        return view;
    }

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

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

}

问题是,当我从第一张图片滚动 3 次到最后一张时,图片不再显示,日志中的内存不足。请帮帮我!!!

我发现了问题。 因为我每次都创建了新视图。

所以现在我做下一步:

 list= createListOfImages();
    mPager=(ViewPager)findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(list);
    mPager.setAdapter(mPagerAdapter);

还有我的适配器:

   private class ScreenSlidePagerAdapter extends  PagerAdapter {
    List<View> pages;

    public ScreenSlidePagerAdapter(List<View> pages){
        this.pages=pages;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(pages.get(position));
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i(TAG,"show image "+position);
        View v = pages.get(position);
        container.addView(v);
        return v;
    }

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

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }
}

当然我会做下一个图像:

 @Override
protected void onDestroy() {
    super.onDestroy();
    for (int i=0;i<Constants.imageUrls.size();i++){
        ImageView imageView = (ImageView)list.get(i).findViewById(R.id.image);
        imageView.setImageDrawable(null);
    }
}