Fallenreaper
Fallenreaper

Reputation: 10704

when going back and forth from an activity which shows an image, will get memory error. not freeing image?

I have an activity which is called. This activity will show an image. The issue is that if i go to the previous activity within the same application and then recall it. If i do it enough times, i will get a memory error. Is there a way to make it so that it will free up that space when you go back/end the activity?

I thought it freed automatically, but maybe it doesnt ? Is there a call that I should be doing? Like onDestroy(){ image.recycle(); } and all that such stuff?

I figure there is something going on here.

Exception

10-16 11:50:27.778: E/AndroidRuntime(18255): FATAL EXCEPTION: main
10-16 11:50:27.778: E/AndroidRuntime(18255): java.lang.OutOfMemoryError
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at com.bentley.cordova.plugins.ZoomImageActivity$1.run(ZoomImageActivity.java:111)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.os.Handler.handleCallback(Handler.java:605)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.os.Looper.loop(Looper.java:137)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at android.app.ActivityThread.main(ActivityThread.java:4697)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at java.lang.reflect.Method.invoke(Method.java:511)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-16 11:50:27.778: E/AndroidRuntime(18255):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 77

Answers (1)

Sebastian Breit
Sebastian Breit

Reputation: 6159

I had this problem and solved it thanks to another stackoverflow user. I don't find the answer, so I'll post it here:

if your layout looks something like this:

<LinearLayout
android:id="@+id/yourcontainer"
...>

more objects

</LinearLayout>

In your onDestroy method, you should unbind all the drawables like this:

@Override
protected void onDestroy() {
    super.onDestroy();
    unbindDrawables(findViewById(R.id.yourcontainer));
}

private void unbindDrawables(View view) {
    if (view == null)
        return;
    if (view.getBackground() != null)
    view.getBackground().setCallback(null);
    if (view instanceof ViewGroup) {
    for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
    }
    try {
        ((ViewGroup) view).removeAllViews();
    } catch (UnsupportedOperationException mayHappen) {
        Log.e("Error:", mayHappen.getMessage());
        }
    }
}

Upvotes: 2

Related Questions