解决 out of memory 的方法

Bertha 。 2022-09-26 01:51 233阅读 0赞
  1. private Bitmap decodeBitmap(File file){
  2. try{
  3. BitmapFactory.Options o = new BitmapFactory.Options();
  4. o.inJustDecodeBounds = true;
  5. BitmapFactory.decodeStream(new FileInputStream(file),null,o);
  6. Bitmap currentBitmap = Bitmap.createBitmap(o.outWidth,o.outHeight, Bitmap.Config.ARGB_8888);
  7. final int REQYIRED_SIZE = 800;
  8. int scale = 1;
  9. while (o.outWidth / 2 / scale >= REQYIRED_SIZE && o.outHeight / 2 /scale >= REQYIRED_SIZE){
  10. scale *= 2;
  11. }
  12. //Log.e(TAG, "decodeBitmap: scale---"+scale);
  13. o.inJustDecodeBounds = false;
  14. o.inSampleSize = scale;
  15. o.inMutable = true;
  16. o.inBitmap = currentBitmap;
  17. return BitmapFactory.decodeStream(new FileInputStream(file),null,o);
  18. //return currentBitmap;
  19. }catch (Exception e){
  20. e.printStackTrace();
  21. }
  22. return null;
  23. }

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读