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

前端 未结 30 2139
暗喜
暗喜 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:59

    Use an image loading library like Picasso or Glide. Using these libraries will prevent crashes in the future.

    0 讨论(0)
  • 2020-11-21 08:02

    Your app is crashing because your image size (in MB Or KB) is too large so it is not allocating space for that. So before pasting your image in drawable just reduce the size.

    OR

    You can add Following in application tag at Manifest.xml

     android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:allowBackup="true"
    

    After Adding this App will not Crash.

    • always use less sizes images in App.
    • If You adding large sizes of images in app , you should add above syntex, but App size will increase.
    0 讨论(0)
  • 2020-11-21 08:02

    I don't really recommend editing manifest like this

    android:hardwareAccelerated="false" , android:largeHeap="true"

    These options cause not smooth animation effect on your app. Moreover 'liveData' or changing your local DB(Sqlite, Room) activate slowly. It is bad for user experience.

    So I recommend RESIZE bitmap

    Below is the sample code

    fun resizeBitmap(source: Bitmap): Bitmap {
          val maxResolution = 1000    //edit 'maxResolution' to fit your need
          val width = source.width
          val height = source.height
          var newWidth = width
          var newHeight = height
          val rate: Float
    
                if (width > height) {
                    if (maxResolution < width) {
                        rate = maxResolution / width.toFloat()
                        newHeight = (height * rate).toInt()
                        newWidth = maxResolution
                    }
                } else {
                    if (maxResolution < height) {
                        rate = maxResolution / height.toFloat()
                        newWidth = (width * rate).toInt()
                        newHeight = maxResolution
                    }
                }
                return Bitmap.createScaledBitmap(source, newWidth, newHeight, true)
     }
    
    0 讨论(0)
  • 2020-11-21 08:03

    This should work

     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inSampleSize = 8;
    
     mBitmapSampled = BitmapFactory.decodeFile(mCurrentPhotoPath,options);
    
    0 讨论(0)
  • 2020-11-21 08:05

    I'm a newbie in android developing but I hope my solution helps, it works on my condition perfectly. Im using Imageview and set it's background to "src" because im trying to make a frame animation. I got the same error but when I tried to code this it worked

    int ImageID = this.Resources.GetIdentifier(questionPlay[index].Image.ToLower(), "anim", PackageName);
                imgView.SetImageResource(ImageID);
                AnimationDrawable animation = (AnimationDrawable)imgView.Drawable;
                animation.Start();
                animation.Dispose();
    
    0 讨论(0)
  • 2020-11-21 08:08

    I got below error

    "E/art: Throwing OutOfMemoryError "Failed to allocate a 47251468 byte allocation with 16777120 free bytes and 23MB until OOM"

    after adding android:largeHeap="true" in AndroidManifest.xml then I rid of all the errors

    <application
        android:allowBackup="true"
        android:icon="@mipmap/guruji"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:theme="@style/AppTheme">
    
    0 讨论(0)
提交回复
热议问题