sofi37 sofi37 - 5 days ago 5
Android Question

Out of memory Exception using Picasso

I need to show images in view pager. To show image in image view I use Picasso library. Also I resize image using screen size.

So in my Activity in OnCreate I got view pager:

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


Then get device size:

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


And here is my Page Adapter:

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

}

}


The problem is that when I scroll 3 times from first image to last then images not showing anymore and I have out of memory in logs. Please help me!!!

Answer

I found the problem. It was because every time I created new view.

So now I do next:

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

And my adapter:

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

And of cource for image I do the next :

 @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);
    }
}
Comments