Resize a large bitmap file to scaled output file on Android

前端 未结 21 1285
执念已碎
执念已碎 2020-11-22 05:51

I have a large bitmap (say 3888x2592) in a file. Now, I want to resize that bitmap to 800x533 and save it to another file. I normally would scale the bitmap by calling

21条回答
  •  醉梦人生
    2020-11-22 06:16

    I used code like this:

      String filePath=Environment.getExternalStorageDirectory()+"/test_image.jpg";
      BitmapFactory.Options options=new BitmapFactory.Options();
      InputStream is=new FileInputStream(filePath);
      BitmapFactory.decodeStream(is, null, options);
      is.close();
      is=new FileInputStream(filePath);
      // here w and h are the desired width and height
      options.inSampleSize=Math.max(options.outWidth/460, options.outHeight/288); //Max 460 x 288 is my desired...
      // bmp is the resized bitmap
      Bitmap bmp=BitmapFactory.decodeStream(is, null, options);
      is.close();
      Log.d(Constants.TAG, "Scaled bitmap bytes, "+bmp.getRowBytes()+", width:"+bmp.getWidth()+", height:"+bmp.getHeight());
    

    I tried original image is 1230 x 1230, and got bitmap says is 330 x 330.
    And if tried 2590 x 3849, I'll got OutOfMemoryError.

    I traced it, it still throw OutOfMemoryError on line "BitmapFactory.decodeStream(is, null, options);", if original bitmap too large...

提交回复
热议问题