Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of memory error when using View Pager to load images. #56

Closed
jfclifton opened this issue Apr 1, 2013 · 5 comments
Closed

Out of memory error when using View Pager to load images. #56

jfclifton opened this issue Apr 1, 2013 · 5 comments

Comments

@jfclifton
Copy link

Hello, i'm rather new at Android development. I'm using a view pager to load in an array of Urls for displaying. I am getting an out of memory error when swiping through the images. I do not notice any patterns when this happens, But I do notice it if I swipe fast. This is my first time implementing a View Pager so I may be missing something important. Has anyone ever used UrlImageViewHelper with a view pager? I will paste the code I have to see if we can figure out the root of the problem. I feel like the most important piece I am missing is with the PagerAdapter. Here is the code:

private class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<String> images = new ArrayList<String>();
    private LayoutInflater inflater;

    ImagePagerAdapter(ArrayList<String> imageUrls) {
        this.images = imageUrls;
        inflater = getLayoutInflater();
    }

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


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


    @Override
    public Object instantiateItem(final ViewGroup view, int position) {
        final View imageLayout = inflater.inflate(R.layout.activity_imagedetailitem, view, false);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
        spinner.setVisibility(View.VISIBLE);

        UrlImageViewHelper.setUrlDrawable(imageView, imageUrls.get(position), null, UrlImageViewHelper.CACHE_DURATION_ONE_DAY, new UrlImageViewCallback() {

            @Override
            public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url,
                    boolean loadedFromCache) {

                if (loadedBitmap == null) {
                    //Error loading image
                    Toast.makeText(TLImageDetailsActivity.this, "Failed to load image", Toast.LENGTH_SHORT).show();
                } else {
                    System.out.println("Successfully loaded image");
                    //Image was successfully loaded
                    ((ViewPager) view).addView(imageLayout, 0);

                    spinner.setVisibility(View.GONE);
                }

            }
        });
        return imageLayout;

    }


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

    @Override  
       public void finishUpdate(View arg0) {}  

       @Override  
       public void restoreState(Parcelable arg0, ClassLoader arg1) {}  

       @Override  
       public Parcelable saveState() {  
           return null;  
       }  

       @Override  
       public void startUpdate(View arg0) {}  

     }  //end of page adapter

Here is the output from LogCat:
04-01 16:37:30.212: E/AndroidRuntime(18279): FATAL EXCEPTION: AsyncTask #3
04-01 16:37:30.212: E/AndroidRuntime(18279): java.lang.RuntimeException: An error occured while executing doInBackground()
04-01 16:37:30.212: E/AndroidRuntime(18279): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.lang.Thread.run(Thread.java:1027)
04-01 16:37:30.212: E/AndroidRuntime(18279): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7111KB, Allocated=3616KB, Bitmap Size=25807KB)
04-01 16:37:30.212: E/AndroidRuntime(18279): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-01 16:37:30.212: E/AndroidRuntime(18279): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:694)
04-01 16:37:30.212: E/AndroidRuntime(18279): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.loadBitmapFromStream(UrlImageViewHelper.java:117)
04-01 16:37:30.212: E/AndroidRuntime(18279): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.access$3(UrlImageViewHelper.java:94)
04-01 16:37:30.212: E/AndroidRuntime(18279): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$1.onDownloadComplete(UrlImageViewHelper.java:589)
04-01 16:37:30.212: E/AndroidRuntime(18279): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:655)
04-01 16:37:30.212: E/AndroidRuntime(18279): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:1)
04-01 16:37:30.212: E/AndroidRuntime(18279): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-01 16:37:30.212: E/AndroidRuntime(18279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-01 16:37:30.212: E/AndroidRuntime(18279): ... 4 more

Thank you so much for providing me with your helper class :) (sorry for a big wall of mess, hopefully it's not too painful)

@koush
Copy link
Owner

koush commented Apr 1, 2013

can you turn logging on in the library (see Constants.java) and attach a full log?

@jfclifton
Copy link
Author

How to assign logging true within code?

thanks

@FrancescLlao
Copy link

going to Constants.java and setting to true this line:
public static final boolean LOG_ENABLED = false;

@koush
Copy link
Owner

koush commented Apr 25, 2013

Closing since it is stale.

I don't thikn you should construct the view in instantiateItem like that. Override getItem to return a Fragment, and then override getView to return a view. Returning a view in instantiateItem means that references may be held keeping the images in memory.

@koush koush closed this as completed Apr 25, 2013
@anikikvn
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants