Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
Solution 1:
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
Solution 2:
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" >
Solution 3:
For me, the problem was that my .png file was being de-compressed to be a really huge bitmap in memory, because the image had very large dimensions (even though the file size was tiny).
So the fix was to simply resize the image :)
Solution 4:
Actually you can add in your manifest these lines android:hardwareAccelerated="false"
, android:largeHeap="true"
it is working for some situations, but be aware that the other part of code can be arguing with this.
<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">
Solution 5:
This should work
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mBitmapSampled = BitmapFactory.decodeFile(mCurrentPhotoPath,options);