В приложении «Моя камера» я открываю галерею с этим кодом:
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();
}