Android:以编程方式释放位图内存资源

时间:2013-12-21 04:33:38

标签: android memory bitmap dialog

我有一个对话框,用户可以在对话框中自由绘制。

对话框正在扩展视图,绘图区域由

创建
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);

但是,不需要保留绘图,当用户关闭对话框并再次重新打开时,不需要保留他之前的绘图并从null重绘。

详细代码:

DoodleView:

   // 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<Integer, Path>();
      previousPointMap = new HashMap<Integer, Point>();
   } // 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();
   }

写作板:

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;
            }
        });

问题:

我发现在多次打开对话框后(使用三星Note2进行7到8次),对话框会滞后几秒而手机没有响应,然后再进一步再按一下,对话框会再次出现一切都很好。

此时的logcat报告E/OpenGLRenderer(25296): Out of memory!

并且它持续了一段时间没有停止。

然而,如果进一步按下绘图板,则会出现严重的内存错误,这次系统会挂起。

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)

我已经研究并添加了 每当用户关闭对话框时bitmap.recycle();。但它仍然报告同样的错误。

当用户关闭对话框时,有没有办法处理位图?

谢谢!

4 个答案:

答案 0 :(得分:15)

您应该在解除对话

时调用此方法
bitmap.recycle();
bitmap = null;

bitmap.recycle();释放位图中使用的本机堆。 将其设置为null是为了帮助GC快速收集您的参考。

答案 1 :(得分:7)

首先使画布或任何其他对象必须位图的引用无效

if (bitmapCanvas != null) {
    bitmapCanvas.setBitmap(null);
    bitmapCanvas = null;
}

第二次发布位图资源

if (bitmap != null) {
    bitmap.recycle();
    bitmap = null;
}

如果需要,请将位图保留为WeakReference

WeakReference<Bitmap> bitmap;
if (bitmap != null) {
    bitmap.get().recycle();
    bitmap.clear();
    bitmap= null;
}

答案 2 :(得分:3)

看起来一次只能在内存中存在一个位图。一些想法。

首先,当回收位图时将其设置为null,同时将画布设置为null。如果这不能解决问题,我很想看到你用来实际绘制线条的代码。在调用对话框并关闭它之后获得堆转储将显示哪些对象仍在内存中,并允许您快速跟踪位图上的内容,这很有趣:)

如果您之前没有这样做,post是调查堆转储的一个很好的开始。

更新

要将画布设置为null,您可以执行类似这样的操作

public void recycling()
{
   bitmap.recycle();
   bitmap = null;
   bitmapCanvas = null;
}

然而,仔细检查您的代码后,我发现您每次都在创建一个新对话框,然后解除对话框。这不会从内存中释放对话框。尝试更新代码,这样只有在对话框为空时才初始化对话框。

public void write_board () 
 {
  if(writing_dialog == null){
    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.setVolumeControlStream(AudioManager.STREAM_MUSIC);
 }
 //now do stuff with your dialog
 writing_dialog.show();


 doodleView = (DoodleView) writing_dialog.findViewById(R.id.doodleView);

答案 3 :(得分:0)

位图内存是一个非常常见的问题。试试这个帖子中的一些解决方案

Strange out of memory issue while loading an image to a Bitmap object