Reputation: 23596
In My Camera Application, I open gallery with this code:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
All run good. But while i go back from the Gallery then the Android camera not showing its preview. Why it is happend? And Whats Wrong in My code ?
Code for My Camera Activity:
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();
}
Upvotes: 1
Views: 2425
Reputation: 4245
You need to release the camera everytime your activity is paused, so that others can also use the camera.. and reacquire the camera in the onResume call..
Here is the code for the onPause, where mCameraDev is the handle to the camera object
@Override
protected void onPause() {
Log.w(TAG, "OnPause()");
if (null != mCameraDev) {
mCameraDev.stopPreview();
mCameraDev.release();
mCameraDev = null;
}
super.onPause();
}
Here is the code for the 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;
}
}
Upvotes: 3