Out of Memory Exception while scroll Listview?

前端 未结 6 1067
温柔的废话
温柔的废话 2020-12-18 15:36

I refered through many resources but can\'t get right answer,

I have made an custom adapter to view images in a listview. This images are retrieved from memory card.

相关标签:
6条回答
  • 2020-12-18 16:16

    Actually I think Best thing is to find reson for error. Main reson for this "Out of Memroy" is that when u scroll gcc is allocating memory even you scroll up from down it will allocate separate memory for listview in other words ur memeory is getting wasted or redundantl used. Even in bitmap u can reduce emeroy allocation size by scalesize option but it is jsut short term solution for permanent solution i find out create a view array for every listview by finding out the size i.e. number of images in d card and when you scroll compare poistion with size of view when poistion get equal to size of view-1 u have reached end net unique memeroy has been allocated ,no separete memroy will be allocated in listview if u again scroll up or down coz u have reached to end of view array ,no furthur need for allocating memeroy, hence problem solved

    0 讨论(0)
  • 2020-12-18 16:23

    Out of memory means well you need to load images in the listview only when they're in view and release them when not in view - load them dynamically.

    You can also try incorporating a ViewHolder class in that adapter.

    0 讨论(0)
  • 2020-12-18 16:24

    May be you can try this:

    Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    iv.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
    

    Hope it Helps!!

    0 讨论(0)
  • 2020-12-18 16:29

    This is happening because you are creating bitmaps. when app heap goes full it gives out of memory. For decoding your bitmaps use this method- Out of memory while creating bitmaps on device

    It will surely help you out.

    0 讨论(0)
  • 2020-12-18 16:37

    use this concept this will help you, After that set the imagebitmap on image view

    public static Bitmap convertBitmap(String path)   {
    
            Bitmap bitmap=null;
            BitmapFactory.Options bfOptions=new BitmapFactory.Options();
            bfOptions.inDither=false;                     //Disable Dithering mode
            bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            bfOptions.inTempStorage=new byte[32 * 1024]; 
    
    
            File file=new File(path);
            FileInputStream fs=null;
            try {
                fs = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
            try {
                if(fs!=null)
                {
                    bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
                }
                } catch (IOException e) {
    
                e.printStackTrace();
            } finally{ 
                if(fs!=null) {
                    try {
                        fs.close();
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
                }
            }
    
            return bitmap;
        }
    

    If you want to make a small image from large image with height and width like 60 and 60 and scroll the listview fast then use this concept

    public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
                int reqHeight) {
    
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
    
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            Bitmap bmp = BitmapFactory.decodeFile(path, options);
            return bmp;
            }
    
        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
    
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float) height / (float) reqHeight);
                } else {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
                 }
             }
             return inSampleSize;
            }
    

    I hope it will help you much.

    You cam take help from developer site Here

    0 讨论(0)
  • 2020-12-18 16:40

    You are using

    Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath())
    

    to load bitmap.. which can load large bitmaps into the memory, make sure that you load them in required size only.

    Follow Loading Large Bitmaps Efficiently

    It is also a good practice to load these bitmaps in AsyncTask.

    For that follow Processing Bitmaps Off the UI Thread

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