Out of memory while creating bitmaps on device

前端 未结 2 958
执笔经年
执笔经年 2020-11-29 13:43

im having problems with high resolution images.

Im using nodpi-drawable folder for 1280x720 images, and using this code to scale it.

public static Dr         


        
相关标签:
2条回答
  • 2020-11-29 14:20

    You should use the utility class BitmapFactory for image-processing-operations. Also use BitmapFactory.Options to adjust the input/output sizes of the bitmaps. After a bitmap is not needed anymore, you should free the related memory.

    0 讨论(0)
  • 2020-11-29 14:33

    use this method to resize your bitmap-

     Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);
    

    use this Defination-

     public 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 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;
    }
    

    If you are using resource then replace method with BitmapFactory's decodeResource method..

     public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {
    
    ....
    .....
    return BitmapFactory.decodeResource(res, resId, options);
    }
    
    0 讨论(0)
提交回复
热议问题