我正在尝试为我的应用开发自定义相机应用程序。在少数型号(例如,Xiomi Red mi,Samsung core2 duos)中,当相机应用程序启动并且它崩溃时,它会出现问题。
我的活动:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;
static Context mcontext;
List<Size> mSupportedPreviewSizes;
private String meetingImgName="";
private ArrayList<Promoter> promoterList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_camera_activity);
Bundle mBundle =getIntent().getExtras();
promoterList = (ArrayList<Promoter>) mBundle
.getSerializable("PromoterList");
mcontext=this;
mCamera = getCameraInstance();
/*List<Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
params.setPictureSize(size.width, size.height);*/
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mCameraPreview);
//Adding listener
ImageView captureButton = (ImageView) findViewById(id.button_capture);
ImageView cancelButton=(ImageView)findViewById(R.id.button_cancel);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
List<String> supportedFocusModes = mCamera.getParameters().getSupportedFocusModes();
boolean hasAutoFocus = supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO);
if(hasAutoFocus){
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
mCamera.takePicture(shutterCallback, null, mPicture);
}
});
}else{
mCamera.takePicture(shutterCallback, null, mPicture);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
/**
* Helper method to access the camera returns null if
* it cannot get the camera or does not exist
* @return
*/
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
ShutterCallback shutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
}
};
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
................................
.........................
@Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(mCameraPreview.getHolder());
mCamera.startPreview();
} catch (Exception e) {
Toast.makeText(CameraActivity.this, "Unable to open camera.", Toast.LENGTH_LONG)
.show();
}
}
}
}
预览课程
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
private boolean previewing = false;
private Context mcon;
private Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
// Constructor that obtains context and camera
public CameraPreview(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mcon = context;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this); // we get notified when
// underlying surface is created
// and destroyed
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// this is adeprecated method, is not requierd after 3.0
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
mSupportedPreviewSizes = mCamera.getParameters()
.getSupportedPreviewSizes();
Camera.Parameters params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setExposureCompensation(0);
params.setPictureFormat(ImageFormat.JPEG);
params.setJpegQuality(100);
List<String> focusModes = params.getSupportedFocusModes();
if (focusModes
.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Toast.makeText(mcon, "auto focus supported", Toast.LENGTH_LONG)
.show();
Log.i("auto focus supported", "auto focus");
// set the focus mode
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// set Camera parameters
} else {
Log.i("auto focus not supported", "Not supported");
Toast.makeText(mcon, " auto focus not supported",
Toast.LENGTH_LONG).show();
}
mCamera.setParameters(params);
} catch (IOException e) {
// left blank for now
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
previewing = false;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// We purposely disregard child measurements because act as a
// wrapper to a SurfaceView that centers the camera preview instead
// of stretching it.
final int width = resolveSize(getSuggestedMinimumWidth(),
widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(),
heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width,
height);
}
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
if (previewing) {
mCamera.stopPreview();
previewing = false;
}
if (mCamera != null) {
// start preview with new settings
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width,
mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
previewing = true;
} catch (Exception e) {
// intentionally left blank for a test
}
}
}
}
这是崩溃日志cat:
我的代码中有什么问题?