【问题标题】:Heap memory is growing more after taking picture using Camera使用相机拍照后堆内存增长更多
【发布时间】:2012-10-10 19:55:49
【问题描述】:

在我的应用程序中使用相机时出现内存问题。我用 SurfaceView 拍照。在 log cat 中拍照后,它显示堆内存增长到 30 mb,有时也增长到 40mb。因此,有时应用程序也会崩溃“OutofMemoryException”。在这里我提供我的代码。请帮帮我。

surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


capture.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                    myPictureCallback_JPG);
            capture.setClickable(false);
        }
    });



PictureCallback myPictureCallback_RAW = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        System.out.println("myPictureCallback_RAW");
    }
};




 PictureCallback myPictureCallback_JPG = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {


        try {

            bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
                    arg0.length);

        }
        catch (OutOfMemoryError e) {
                Toast.makeText(FromCamera.this, "Please try again",
                        Toast.LENGTH_SHORT).show();
        }catch (Exception e) {
            Toast.makeText(FromCamera.this, "Please try again",
                    Toast.LENGTH_SHORT).show();
        }

        capture.setEnabled(false);
        buttonBack.setVisibility(View.VISIBLE);
        buttonDone.setVisibility(View.VISIBLE);
        buttonBack.setEnabled(true);
        buttonDone.setEnabled(true);
    }
};



ShutterCallback myShutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() {
        System.out.println("onShutter");
    }
};

我试过强制垃圾回收,但还是没用。

public void surfaceDestroyed(SurfaceHolder holder) {
    if (camera != null) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }
    previewing = false;
}

【问题讨论】:

标签: android memory-leaks out-of-memory heap-memory


【解决方案1】:

您应该检查您的应用程序的内存分配,请参阅相关帖子here(它还有一些很棒的文章链接在最后)。

【讨论】:

    【解决方案2】:

    你是在使用后释放你的相机吗?完成拍照后,尝试使用 camera.release() 释放相机,并再次评估内存使用情况。

    您可以尝试的另一件事是在使用后破坏表面,看看是否有帮助。

    【讨论】:

    • 我正在释放相机并破坏表面...检查我帖子中的更新代码
    • 能不能放一些日志看看,是不是真的进入了surfaceDestroyed方法?
    【解决方案3】:

    当你处理 Bitmap 时,垃圾收集器可能会失败一段时间......当你不使用 bitmap 时,请确保释放 Bitmap 内存

    if(!mBitmap.isRecycled())
    mBitmap.recycle();
    

    释放位图内存

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多