change bitmap resolution in Android app

后端 未结 3 1901
日久生厌
日久生厌 2021-01-14 08:16

I\'m writing an application that uses the phone\'s camera to take a picture, and then use it in my app. The thing is, the app runs out of memory, and it is probably because

3条回答
  •  被撕碎了的回忆
    2021-01-14 09:17

    You can Set Its Width and Height

    Bitmap bm = ShrinkBitmap(imagefile, 150, 150);
    

    Function to Call

    Bitmap ShrinkBitmap(String file, int width, int height){
    
     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    
        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
    
        if (heightRatio > 1 || widthRatio > 1)
        {
         if (heightRatio > widthRatio)
         {
          bmpFactoryOptions.inSampleSize = heightRatio;
         } else {
          bmpFactoryOptions.inSampleSize = widthRatio; 
         }
        }
    
        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
     return bitmap;
    }
    

    }

    This are two more links which might Help You. Link 1 & Link 2

提交回复
热议问题