Search code examples
androidandroid-viewpagerandroid-imageviewandroid-imageandroid-memory

Optimizing images inside View Pager


I am having ViewPager with around 10 pages. All pages have images of resolution more than 1000x2000. Basically width of the images are different for different images. As I was not able to place images of more than size 2048 width so I used 2 images(divided one single image to 2 images).So each page has 2 images and other views as well.

I tried many ways of optimizing but could able to optimize much. I tried making ImageView null and even setting its bitmap to null and even removing all views and making them null in on destroy and Optimize bitmaps inside ViewPager

In this I am using Asyntask to load images on screen. And even tried storing images in Cache. But still facing memory related issues.

Needs suggestion to how can I optimize more and avoid out of memory.


Solution

  • I solved this issue by having AsyncTask to load images from resource. And after loading from memory I store it in LRU cache. And then I retrieve it from cache.

    In doInBackgroud() I have:

    BitmapFactory.Options mOptions = new BitmapFactory.Options();
        mOptions.inPurgeable = true;
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        mOptions.inPreferQualityOverSpeed = false;
        mOptions.inJustDecodeBounds = false;
        mOptions.inScaled = true;
    
    
    
            SoftReference<Bitmap> bm = new SoftReference<Bitmap>(
                    BitmapFactory.decodeResource(
                        getApplicationContext().getResources(), data,
                            mOptions));
            return bm.get();
    

    With this, I am recycling images in onDestroy() and making Bitmap to null.