Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

前端 未结 30 2621
暗喜
暗喜 2020-11-21 07:33

I want to show the Bitmap image in ImageView from sd card which is stored already. After run my application is crash and getting Ou

30条回答
  •  半阙折子戏
    2020-11-21 07:56

    stream = activity.getContentResolver().openInputStream(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    
    bitmap = BitmapFactory.decodeStream(stream, null, options);
    int Height = bitmap.getHeight();
    int Width = bitmap.getWidth();
    enter code here
    int newHeight = 1000;
    float scaleFactor = ((float) newHeight) / Height;
    float newWidth = Width * scaleFactor;
    
    float scaleWidth = scaleFactor;
    float scaleHeight = scaleFactor;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    resizedBitmap= Bitmap.createBitmap(bitmap, 0, 0,Width, Height, matrix, true);
    bitmap.recycle();
    

    Then in Appliaction tag, add largeheapsize="true

提交回复
热议问题