View Pager with Universal Image Loader Out of Memory Error

后端 未结 7 1555
别跟我提以往
别跟我提以往 2021-02-02 01:58

I am not really sure if a ViewPager with Universal Image Loader can/should be used as an alternate for a gallery like interface since I have run into an Out of Memory error whil

相关标签:
7条回答
  • 2021-02-02 02:19

    I had this problem when simply setting Uri to ImageView using: iv.setImageURI(Uri.fromFile(imgFile)); I had the same problem with Universal Image Loader, and I even looked for other Image Loaders out there, and found another good one called "Picasso", but it also had the same problem.

    So what worked for me is using GestureImageView and setting gesture-image:recycle to true through XML, and load the images with the following code:

                Drawable yourDrawable = null;
    
                try {
                    InputStream inputStream = getActivity().getContentResolver().openInputStream(Uri.fromFile(img));
                    yourDrawable = Drawable.createFromStream(inputStream, Uri.fromFile(img).toString() );
                    inputStream.close();
                } catch (FileNotFoundException e) {
                    yourDrawable = getResources().getDrawable(R.drawable.ic_launcher);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                if (yourDrawable != null)
                    iv.setImageDrawable(yourDrawable);
    

    the reason it was crashing and giving OOM error is that the bitmaps aren't recycled when the image aren't displayed on the screen anymore, hence a memory leak occurs.

    If there is another way to recycle the bitmap in the normal ImageView, that would be a better solution.

    Hope I helped.

    0 讨论(0)
提交回复
热议问题