App crashes after taking multiple pictures from Android camera

后端 未结 3 2021
陌清茗
陌清茗 2020-12-16 08:23

I am developing an application for Android which is supposed to take pictures from camera and use those images as profile pictures for the app. However, the app runs fine fo

相关标签:
3条回答
  • 2020-12-16 09:06

    Finally I solved the problem myself. The main problem which was causing memory leak is that I was not recycling the bitmap objects. I was just replacing the bitmap object with another bitmap image. Thought the object got replaced by another Bitmap image, still the previous Bitmap image existed in the memory. So, on taking multiple images from Camera, Bitmap images piled up and raised an exception of out-of-memory.

    Therefore, I played a trick to recycle(delete) the Bitmap image before assigning a new image to the Bitmap object. I just used

    mImageBitmap.recycle();
    

    This flushed away the previous Bitmap image and no memory leak occurred. Hope it helped others as well.

    0 讨论(0)
  • 2020-12-16 09:16

    Try scaling your larger bitmaps using the inSampleSize as follows: Snippet taken from @Fedor's lazy list https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/ImageLoader.java

    public int inSampleSize

    If set to a value > 1, requests the decoder to subsample the original image, returning asmaller image to save memory.

    //decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f){
            try {
                //decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                FileInputStream stream1=new FileInputStream(f);
                BitmapFactory.decodeStream(stream1,null,o);
                stream1.close();
    
                //Find the correct scale value. It should be the power of 2.
                final int REQUIRED_SIZE=70;
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                int scale=1;
                while(true){
                    if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                        break;
                    width_tmp/=2;
                    height_tmp/=2;
                    scale*=2;
                }
    
                //decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize=scale;
                FileInputStream stream2=new FileInputStream(f);
                Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
                stream2.close();
                return bitmap;
            } catch (FileNotFoundException e) {
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    Hope this help you:

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

    Replace this line

    profilePic.setImageURI(Uri.fromFile(new  File(capturedImageFilePath)));
    

    You are loading images from sdcard directly and it may be large in size that causing memory problem.

    Try this

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize=8;      // 1/8 of original image
    Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath,options);
    profilePic.setImageBitmap(b);
    
    0 讨论(0)
提交回复
热议问题