Catching OutOfMemoryError in decoding Bitmap

前端 未结 4 889
天命终不由人
天命终不由人 2020-12-13 14:30

Is it a good practice to catch OutOfMemoryError even you have tried some ways to reduce memory usage? Or should we just not catching the exception? Which one is better pract

4条回答
  •  Happy的楠姐
    2020-12-13 15:09

    I did something like this: I catch the error only for try to scale down the image until it works. Eventually it can not work at all; then returns null; otherwise, in success, returns the bitmap.

    Outside I decide what to do with the bitmap whether it's null or not.

    // Let w and h the width and height of the ImageView where we will place the Bitmap. Then:
    
    // Get the dimensions of the original bitmap
    BitmapFactory.Options bmOptions= new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds= true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW= bmOptions.outWidth;
    int photoH= bmOptions.outHeight;
    
    // Determine how much to scale down the image. 
    int scaleFactor= (int) Math.max(1.0, Math.min((double) photoW / (double)w, (double)photoH / (double)h));    //1, 2, 3, 4, 5, 6, ...
    scaleFactor= (int) Math.pow(2.0, Math.floor(Math.log((double) scaleFactor) / Math.log(2.0)));               //1, 2, 4, 8, ...
    
    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds= false;
    bmOptions.inSampleSize= scaleFactor;
    bmOptions.inPurgeable= true;
    
    do
    {
        try
        {
            Log.d("tag", "scaleFactor: " + scaleFactor);
            scaleFactor*= 2;
            bitmap= BitmapFactory.decodeFile(path, bmOptions);
        }
        catch(OutOfMemoryError e)
        {
            bmOptions.inSampleSize= scaleFactor;
            Log.d("tag", "OutOfMemoryError: " + e.toString());
        }
    }
    while(bitmap == null && scaleFactor <= 256);
    
    if(bitmap == null)
        return null;
    

    For example, with an image of 3264x2448, the loop iterates 2 times on my phone, and then it works.

提交回复
热议问题