内存不足使用Picasso的例外情况

时间:2015-05-14 17:58:30

标签: android imageview android-viewpager out-of-memory picasso

我需要在视图寻呼机中显示图像。要在图像视图中显示图像,我使用Picasso库。我还使用屏幕尺寸调整图像大小 所以在我的OnCreate活动中,我得到了查看寻呼机:

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次到最后一张图像时,图像不再显示,而且日志中的内存不足。请帮帮我!!!

1 个答案:

答案 0 :(得分:0)

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

所以现在我做下一个:

Permission.GetPermission();

我的适配器:

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

对于图像的cource我会做下一个:

   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;
    }
}