Out of memory error in BitmapFactory

不羁岁月 提交于 2019-12-07 18:26:13

问题


Today I got my app crashed, Out of memory error.

java.lang.OutOfMemoryError
in android.graphics.BitmapFactory.nativeDecodeAsset

I only used Bitmap Factory to make a backgroud to my action bar

The code:

BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.actionbar)); 
         background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 
         actionbar.setBackgroundDrawable(background);

This error doesn't happend on activity start, it happens after changing in activites a lot.

Can someone show me how to fix this

EDIT EDIT EDIT

Here is the error message in developer console:

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3838)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3833)
... 11 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at android.content.res.Resources.getDrawable(Resources.java:1558)
at android.widget.ImageView.resolveUri(ImageView.java:646)
at android.widget.ImageView.setImageResource(ImageView.java:375)
at com.packagename.pp.Activitytwo.disableAnswer(Activitytwo.java:435)
at com.packagename.pp.Activitytwo.submitAnswer(Activitytwo.java:230)
... 14 more

回答1:


You have a memory leak on your code. Consider the use of WeakReference, WeakHashMap or SoftReference to avoid strong references. Free unused resources and variables on onLowMemory method of activities.

Also you can use the option BitmapFactory.Options to decode the bitmap as show the examples here.

I posted the some hints to solve this problem here.




回答2:


first try commenting this code and see if that alone is the issue. It could be something else that might be using up the memory.



来源:https://stackoverflow.com/questions/25533206/out-of-memory-error-in-bitmapfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!