在我的應用程序中使用相機時出現內存問題。我用SurfaceView拍照。在登錄貓拍攝照片後,它顯示堆內存增長到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;
}
我遇到了同樣的問題。你解決了嗎?請回復。提前感謝。 – jagdish
我使用此鏈接提供的技巧解決了它http://developer.android.com/training/displaying-bitmaps/index.html – Aju
感謝您的回覆。 – jagdish