在我的相机应用程序中,我使用以下代码打开图库:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
一切运行良好。但是当我从画廊回来时,Android相机没有显示其预览。 为什么会这样?我的代码中的错误是什么?
我的相机活动代码:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.e(TAG, "onCreate");
Bundle extras = getIntent().getExtras();
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
clickMe = (ImageButton)findViewById(R.id.clickme);
flashBtn = (Button)findViewById(R.id.flashBtn);
selectCamera = (Button)findViewById(R.id.selectCameraFace);
sharePicture = (Button)findViewById(R.id.sharePicture);
selectPicture = (ImageButton)findViewById(R.id.selectPicture);
//mSurfaceView.setOnClickListener(this);
flashBtn.setOnClickListener(this);
selectCamera.setOnClickListener(this);
sharePicture.setOnClickListener(this);
selectPicture.setOnClickListener(this);
clickMe.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
Intent mIntent = new Intent();
StoreByteImage(mContext, imageData, 100, "ImageName");
mCamera.startPreview();
setResult(FOTO_MODE, mIntent);
//finish();
}
}
};
protected void onResume() {
Log.e(TAG, "onResume");
super.onResume();
if(mPreviewRunning)
{
mCamera.stopPreview();
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
protected void onStop() {
Log.e(TAG, "onStop");
super.onStop();
//mCamera.stopPreview();/////// Added Method
}
public void surfaceCreated(SurfaceHolder holder) {
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
// parameters.getSupportedColorEffects();
// parameters.setColorEffect(parameters.EFFECT_NEGATIVE);
mCamera.setParameters(parameters);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
答案 0 :(得分:3)
每次暂停活动时都需要释放相机,以便其他人也可以使用相机..并在onResume调用中重新获取相机..
这是onPause的代码,其中mCameraDev是相机对象的句柄
@Override
protected void onPause() {
Log.w(TAG, "OnPause()");
if (null != mCameraDev) {
mCameraDev.stopPreview();
mCameraDev.release();
mCameraDev = null;
}
super.onPause();
}
以下是onResume的代码
@Override
protected void onResume() {
super.onResume();
if (null != mCameraDev) {
mCameraDev.startPreview();
mIsPreviewing = true;
} else {
mCameraDev = android.hardware.Camera.open();
try {
mCameraDev.setPreviewDisplay(mSurfaceHolder);
} catch (Throwable ex) {
throw new RuntimeException("setPreviewDisplay failed", ex);
}
mIsPreviewing = true;
}
}