java.lang.out of memory error Android Bitmap Factory
A've tried to recycle the bitmap and everything else that I found on google, but the problem is still here... Here is my code:
final SampleView mainPogled = new SampleView(this);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
pozadinap = BitmapFactory.decodeResource(getResources(), R.drawable.nivoapozadina, options);
playButtonp = BitmapFactory.decodeResource(getResources(), R.drawable.play, options);
pozadina = Bitmap.createScaledBitmap(pozadinap, s_width, s_height, false);
playButton = Bitmap.createBitmap(playButtonp);
At the and I draw the bitmaps into a canvas..
Here is the error:
06-14 14:21:16.452: W/dalvikvm(1930): threadid=1: thread exiting with uncaught exception (group=0xb1ab7ba8) 06-14 14:21:16.482: E/AndroidRuntime(1930): FATAL EXCEPTION: main 06-14 14:21:16.482: E/AndroidRuntime(1930): Process: com.hv.snake, PID: 1930 06-14 14:21:16.482: E/AndroidRuntime(1930): java.lang.OutOfMemoryError 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.nativeCreate(Native Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:809) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:786) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:718) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.hv.snake.MainMenu.onCreate(MainMenu.java:56) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.Activity.perfo rmCreate(Activity.java:5231) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.os.Handler.dispatchMessage(Handler.java:102) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.os.Looper.loop(Looper.java:136) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-14 14:21:16.482: E/AndroidRuntime(1930): at java.lang.reflect.Method.invokeNative(Na tive Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): at java.lang.reflect.Method.invoke(Method.java:515) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-14 14:21:16.482: E/AndroidRuntime(1930): at dalvik.system.NativeStart.main(Native Method)
I found the solution after 2 days... This article helped me a lot.
In the onDestroy() method, first I recycle all the bitmaps from the activity, then I set them all to null and after that I call System.gc(). Eg
public void onDestroy() {
super.onDestroy();
background.recycle();
background = null;
System.gc();
}
I hope this will help someone.
链接地址: http://www.djcxy.com/p/33658.html上一篇: 破坏特定活动时释放内存