Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: free up bitmap memory resources programmatically
    primarykey
    data
    text
    <p>I am having a dialog where users can freely draw inside the dialog.</p> <p>The dialog is extending a view, and the drawing area is created by </p> <pre><code>bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); </code></pre> <p>However, the drawing need not to be kept, and when the user close the dialog and reopen again, his previous drawing is not needed to be kept and redraw from null. </p> <h1>Detailed codes:</h1> <h2>DoodleView:</h2> <pre><code> // DoodleView constructor initializes the DoodleView public DoodleView(Context context, AttributeSet attrs) { super(context, attrs); // pass context to View's constructor paintScreen = new Paint(); // used to display bitmap onto screen // set the initial display settings for the painted line paintLine = new Paint(); paintLine.setAntiAlias(true); // smooth edges of drawn line paintLine.setColor(Color.BLACK); // default color is black paintLine.setStyle(Paint.Style.STROKE); // solid line paintLine.setStrokeWidth(25); // set the default line width paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends pathMap = new HashMap&lt;Integer, Path&gt;(); previousPointMap = new HashMap&lt;Integer, Point&gt;(); } // end DoodleView constructor // Method onSizeChanged creates BitMap and Canvas after app displays @Override public void onSizeChanged(int w, int h, int oldW, int oldH) { bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); bitmapCanvas = new Canvas(bitmap); bitmap.eraseColor(Color.parseColor("#80FFFFFF")); // erase the BitMap with white } // end method onSizeChanged // clear the painting public void recycling() { bitmap.recycle(); } </code></pre> <h2>Writing board:</h2> <pre><code>public void write_board () { writing_dialog = new Dialog(Apple.this, android.R.style.Theme_Translucent_NoTitleBar); WindowManager.LayoutParams lp = writing_dialog.getWindow().getAttributes(); lp.dimAmount = 0.5f; writing_dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); Window window = writing_dialog.getWindow(); window.setGravity(Gravity.CENTER); writing_dialog.setContentView(R.layout.alert_drawing_pad); writing_dialog.setCancelable(true); writing_dialog.show(); writing_dialog.setVolumeControlStream(AudioManager.STREAM_MUSIC); doodleView = (DoodleView) writing_dialog.findViewById(R.id.doodleView); alert_close.setOnClickListener(new OnClickListener() { public void onClick(View v) { writing_dialog.dismiss(); doodleView.recycling(); return; } }); </code></pre> <h2>Question:</h2> <p>I discover that after opening the dialog several times (7 to 8 times using Samsung Note2), the dialog would lag for a few second and the phone without response, and then further press again some while later, the dialog can appear again and everything is ok.</p> <p>The logcat at this time reports <code>E/OpenGLRenderer(25296): Out of memory!</code></p> <p>and it goes on without hanging for some time.</p> <p>Yet if further press out for the drawing board, it goes with a serious memory error and this time the system hangs.</p> <pre><code>FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:726) at android.graphics.Bitmap.createBitmap(Bitmap.java:703) at android.graphics.Bitmap.createBitmap(Bitmap.java:670) at com.abc.abc.DoodleView.onSizeChanged(DoodleView.java:60) at android.view.View.sizeChange(View.java:15326) at android.view.View.setFrame(View.java:15290) at android.view.View.layout(View.java:15201) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) at android.view.View.layout(View.java:15204) at android.view.ViewGroup.layout(ViewGroup.java:4793) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) at android.view.View.layout(View.java:15204) at android.view.ViewGroup.layout(ViewGroup.java:4793) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:15204) at android.view.ViewGroup.layout(ViewGroup.java:4793) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) at android.view.View.layout(View.java:15204) at android.view.ViewGroup.layout(ViewGroup.java:4793) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:15204) at android.view.ViewGroup.layout(ViewGroup.java:4793) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2263) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2009) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) at android.view.Choreographer.doCallbacks(Choreographer.java:591) at android.view.Choreographer.doFrame(Choreographer.java:561) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) 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:5493) 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:1209) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I have researched and added<br> <code>bitmap.recycle();</code> whenever the user closes the dialog. But it still reports the same error.</p> <p>Are there any way to dispose the bitmap whenever the user closes the dialog?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload