Sam M
Sam M

Reputation: 159

Out of memory error in BitmapFactory

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

Upvotes: 2

Views: 5608

Answers (2)

Prasanth Kumar S
Prasanth Kumar S

Reputation: 1

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.

Upvotes: 0

Jose Rodriguez
Jose Rodriguez

Reputation: 10162

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.

Upvotes: 5

Related Questions