为什么我的相机在另一个活动开始后停止预览?
在“我的相机应用程序”中,我使用以下代码打开图库:
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次您的 Activity 暂停时,您都需要释放相机,以便其他人也可以使用相机..并在 onResume 调用中重新获取相机..
这是 onPause 的代码,其中 mCameraDev 是相机对象的句柄
这里是 onResume 的代码
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
Here is the code for the onResume