Android: How to solve Bitmap java.lang.OutOfMemoryError?

I get java.lang.OutOfMemoryError when using a bitmap to display large images.
To resize the bitmap, I referred to the following link Save the bitmap and bitmap files .decodeFile ,

My logcat looks like this:

 FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:604) at android.graphics.Bitmap.createBitmap(Bitmap.java:551) at com.android.restaurant.DescriptionPage.getResizedBitmap(DescriptionPage.java:327) at com.android.restaurant.DescriptionPage.setDetailsIntoLayout(DescriptionPage.java:172) at com.android.restaurant.DescriptionPage.onCreate(DescriptionPage.java:101) at android.app.Activity.performCreate(Activity.java:4397) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834) at android.app.ActivityThread.access$500(ActivityThread.java:125) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:132) at android.app.ActivityThread.main(ActivityThread.java:4135) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:491) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method) 
+6
source share
2 answers

Since you have not posted your code, it is difficult to find out if there is a bug or probably inefficient code that can be improved.

But, as a rule, JVM memory is limited, so loading a large image can raise an OutOfMemoryError . You can try to increase the JVM memory with the -Xmx option. This is the first and easiest way.

Another way is to reduce your image (if possible). Or perhaps use a format other than BMP (e.g. JPG). It guarantees good quality and requires less memory.

+5
source

The problem with developing a mobile application is that you have limited memory.

First of all, I try to use PNG (Portable Network graphics) instead of BitMaps (if possible).

Also, it seems like this is an existing issue, please check this out: http://code.google.com/p/android/issues/detail?id=8488

0
source

Source: https://habr.com/ru/post/913295/


All Articles