Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve OutOfMemoryError using LayerDrawable

Error:

java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
at android.content.res.Resources.loadDrawable(Resources.java:1968)
at android.content.res.Resources.getDrawable(Resources.java:677)
at android.widget.ImageView.resolveUri(ImageView.java:542)
at android.widget.ImageView.setImageResource(ImageView.java:315)
at app.Main.GridActivity.initaliseLevel(GridActivity.java:245)
at app.Main.GridActivity.load_player_settings(GridActivity.java:180)
at app.Main.GridActivity.onResume(GridActivity.java:79)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1158)
at android.app.Activity.performResume(Activity.java:4607)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2448)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2486)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)

The images are set by:

ImageButton b0 = (ImageButton) findViewById(R.id.ImageButton00);
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.image);
layers[1] = r.getDrawable(R.drawable.imagetwo);
LayerDrawable layerDrawable = new LayerDrawable(layers);
b0.setImageDrawable(layerDrawable);

From reading other answers, I gathered that the images need to be recycled after use. Is this the best option? If so how can a LayerDrawable be converted to a bitmap, so it can be recycled?

like image 673
Isaac Avatar asked Nov 04 '22 15:11

Isaac


1 Answers

In your manifest > Application Attributes section there is a "Large Heap" setting, select your activity in the Application Nodes and then select "true" in the Heap Settings that did help me until i solved the leak.

and Also i have this in my activity:

private void unbindDrawables(View view) {
    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));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

    @Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.nameslayout));
    System.gc();
}

hop that helps

like image 150
Eddie Kamand Avatar answered Nov 12 '22 21:11

Eddie Kamand