java.lang.OutOfMemoryError - BitmapFactory.decode(strPath)

后端 未结 5 1234
[愿得一人]
[愿得一人] 2020-11-29 22:21

I am getting java.lang.OutOfMemoryError, whenever i am calling UploadActivity.java

Line Number 176 is:

5条回答
  •  暖寄归人
    2020-11-29 23:05

    You need to recycle Bitmap object .

        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);
    

    After above lines of code in your get view just add the code written below ///now recycle your bitmap this will free up your memory on every iteration

        if(bm!=null)
       {
         bm.recycle();
         bm=null;
        }
    

    After this also if you are getting same error the

    Replace below code

        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);
    

    with

     final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    
    Bitmap bm = BitmapFactory.decodeFile(strPath,options);
    imageView.setImageBitmap(bm);
    

    Use inSampleSize to load scales bitmaps to memory. Using powers of 2 for inSampleSize values is faster and more efficient for the decoder. However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.

    For more see Loading Large Bitmaps Efficiently

提交回复
热议问题