在onCreate方法中,我来自Android Developer网站上的Camera API示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
getPermission();
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
senSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
preview.addView(mPreview);
mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d("NullException", "Error creating media file, check storage permissions");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("FileNotFoundException", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("IOException", "Error accessing file: " + e.getMessage());
}
safeToTakePicture = true;
}
};
Button captureButton = (Button) findViewById(R.id.button_start);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
while(!safeToTakePicture) {
}
safeToTakePicture = false;
mCamera.takePicture(null, null, mPicture);
}
}
);
}
似乎onCreate()中的takePicture()有效,但onSensorChanged方法中却无效。当我从onSensorChanged方法中删除了takePicture()时,没有错误,并且仍然拍摄了图片:
@Override
public void onSensorChanged(SensorEvent event) {
Sensor mySensor = event.sensor;
if (mySensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
while(!safeToTakePicture) {
}
safeToTakePicture = false;
mCamera.takePicture(null, null, mPicture);
}
}
这是由onSensorChanged()中的takePicture()引起的错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lawrence.focusrangefinder, PID: 23989
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1448)
at android.hardware.Camera.takePicture(Camera.java:1393)
at com.example.lawrence.focusrangefinder.MainActivity.onSensorChanged(MainActivity.java:157)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:964)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
这是onCreate中使用的CameraPreview类,尽管我认为这并不重要:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("Error", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d("Error", "Error starting camera preview: " + e.getMessage());
}
}
}
我不确定为什么会这样,我认为这与同时拍摄多张图片有关,因为onSensorChanged被多次调用。我看着这个问题:java.lang.RuntimeException: takePicture failed,我添加了一个变量来阻止这种情况的发生,但仍然无法正常工作。