Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
OutOfMemoryError is the most common problem that occurs in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.
As mentioned by Aleksey, you can add the below entities in your manifest file android:hardwareAccelerated="false"
, android:largeHeap="true"
it will work for some environments.
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
you should definitely read some of Androids Developer concept's, especially here:Displaying Bitmaps Efficiently
Read all 5 topics and rewrite your code again. If it still doesn't work we will be happy to see what you've done wrong with the tutorial material.
Here some of the possible answers for these type of errors in SOF
Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap
How to solve java.lang.OutOfMemoryError trouble in Android
Android : java.lang.OutOfMemoryError
java.lang.OutOfMemoryError
Solution for OutOfMemoryError: bitmap size exceeds VM budget
Edit: From the comments of @cjnash
For anyone that still had crashes after they added this line, try sticking your image into your res/drawable-xhdpi/ folder instead of your res/drawable/ and this should resolve this issue
Android Out of memory error with newNonMovableArray in loading Drawable
Thanks to @Umesh for reminding me for the question about the working solution:
Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
although i am not using a bitmap, all binary graphics must be include into drawable-xhdpi/ directory (or larger). So although I am not loading aný bitmap data (as kind of bmp files), the UI loads a binary image. Therefore it must pu put into the correct diectory, dependin its size (where binary images are always xhdpi or above).
java.lang.OutOfMemoryError - How do I fix this?
Try to load image using Glide or Picasso.
For Glide
into Project level build.gradle
dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:22.0.0'
}
while displaying image,
Glide.with(context)
.load("imageURL")
.into(ivImg);
For Piccaso
dependencies {
compile 'com.squareup.picasso:picasso:2.5.1'
}
while displaying image,
Picasso.with(context)
.load(imageURL)
.into(ivImg);
Related Topics
Checking If an Android Application Is Running in the Background
Why Does an Image Captured Using Camera Intent Gets Rotated on Some Devices on Android
Re-Launch of Activity on Home Button, But...Only the First Time
Android How to Adjust Layout in Full Screen Mode When Softkeyboard Is Visible
Retrieving Android API Version Programmatically
Do Fragments Really Need an Empty Constructor
Update Data in Listfragment as Part of Viewpager
What Is the Android Uithread (Ui Thread)
How to Add Parameters to Httpurlconnection Using Post Using Namevaluepair
Get Filename and Path from Uri from Mediastore
Google Maps Android API V2 - Interactive Infowindow (Like in Original Android Google Maps)
How to Call Android Contacts List
Handle Textview Link Click in My Android App