我构建了一个简单的相机应用程序(来自互联网上的各种来源,使用 SurfaceHolder)作为原型,以便稍后与更大的项目集成。
该应用程序运行良好。它不会崩溃并正确保存图像。
我开始将它与更大的项目集成。它现在从另一个活动开始(而不是作为主要活动)。它仍然显示预期的相机视图并且不会崩溃。
但是,它不会保存任何拍摄的照片。
独立相机应用程序和集成相机活动的代码是相同的。
我在没有调用 finish() 或启动其他活动的情况下测试了集成的相机代码(只是为了确保它正确输出图像)。
但它仍然不起作用。
有人遇到过这个吗?谢谢。
这是相机活动的源代码:
public class CameraViewActivity extends Activity implements
SurfaceHolder.Callback, OnClickListener {
private static final String SD_CARD_LOC = "/sdcard/";
private static final String FILE_NAME = "image";
private static final String JPEG_FORMAT = ".jpg";
private static final String TAG = "CameraViewActivity";
private Context context = this;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
Camera camera;
boolean isPreviewRunning = false;
@Override
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.camera_view_layout);
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
surfaceView.setOnClickListener(this);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageByteData, Camera c) {
if (imageByteData != null) {
Intent intent = new Intent(CameraViewActivity.this,SharePhotoActivity.class);
if(saveByteImage(context, imageByteData, 50)){
camera.startPreview();
startActivity(intent);
finish();
}
}
}
};
@Override
public void onResume() {
Log.e(TAG, "onResume");
super.onResume();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onStop() {
Log.e(TAG, "onStop");
super.onStop();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e(TAG, "surfaceCreated");
camera = Camera.open();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
if (isPreviewRunning) {
camera.stopPreview();
}
Camera.Parameters parameters = camera.getParameters();
int height = parameters.getPreviewSize().height;
int width = parameters.getPreviewSize().width;
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0: {
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(90);
}
break;
case Surface.ROTATION_180: {
parameters.setPreviewSize(width, height);
}
break;
case Surface.ROTATION_270: {
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(180);
}
break;
case Surface.ROTATION_90: {
parameters.setPreviewSize(width, height);
}
break;
}
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
isPreviewRunning = true;
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
camera.stopPreview();
isPreviewRunning = false;
camera.release();
}
@Override
public void onClick(View arg0) {
camera.takePicture(null, mPictureCallback, mPictureCallback);
}
public static boolean saveByteImage(Context context, byte[] imageByteData, int quality) {
StringBuffer fileUri = new StringBuffer();
fileUri.append(SD_CARD_LOC);
fileUri.append(FILE_NAME);
fileUri.append("-");
fileUri.append(System.currentTimeMillis());
fileUri.append(JPEG_FORMAT);
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap bitmapImage = BitmapFactory.decodeByteArray(imageByteData, 0, imageByteData.length, options);
FileOutputStream fos = new FileOutputStream(fileUri.toString());
BufferedOutputStream bos = new BufferedOutputStream(fos);
bitmapImage.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
更新:我创建了一个只有两个活动的新 android 项目:一个主要活动和相机视图活动。主要活动只是通过单击按钮启动相机视图活动。并且仍然不会保存任何图像。