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

前端 未结 30 2052
暗喜
暗喜 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

    If android:largeHeap="true" didn't work for you then

    1:

    Image Compression. I am using this website

    2:

    Convert images to mdpi,hdpi, xhdpi, xxhdpi, xxxhdpi. I am using this webiste

    Don't remove android:largeHeap="true"!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-21 07:59

    have you tried adding this to your manifest under applications? android:largeHeap="true"?

    like this

      <application
          android:name=".ParaseApplication"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"
          android:largeHeap="true" >
    
    0 讨论(0)
  • 2020-11-21 07:59

    I had the problem too.

    Same with most of others above. The problem is caused by huge image.

    Just resize some images, and no need to change any code.

    0 讨论(0)
  • 2020-11-21 07:59

    My problem solved after adding

     dexOptions {
            incremental true
            javaMaxHeapSize "4g"
            preDexLibraries true
            dexInProcess = true
        }
    

    in Build.Gradle file

    0 讨论(0)
  • 2020-11-21 07:59

    I ran into this problem when I didn't kill off my old activity when moving on to a new activity. I fixed it with finish();

        Intent goToMain = new Intent(this,MainActivity.class);
        startActivity(goToMain);
        finish();
    
    0 讨论(0)
提交回复
热议问题