可能有90%的情况下,回调函数无法触发。
因此我的图片从未被保存。
我做错了什么?
@Override
public boolean onTouchEvent(MotionEvent event)
{
boolean result = super.onTouchEvent(event);
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN)
{
takePicture();
this.finish(); // ERROR IS HERE. Closing down before callback is done.
}
return result;
}
private void takePicture() {
if (mCamera != null)
mCamera.takePicture(shutterCallback, null, jpegCallback);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
AudioManager meng = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
int volume = meng.getStreamVolume( AudioManager.STREAM_NOTIFICATION);
if (volume != 0)
{
MediaPlayer _shootMP = MediaPlayer.create(getBaseContext(), Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
_shootMP.start();
}
Toast.makeText(CameraActivity.this, "Picture Taken", Toast.LENGTH_SHORT).show();
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
// TODO Do something with the image RAW data.
int test = 1;
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
File photo=new File(Environment.getExternalStorageDirectory(), "photo1.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(_data);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
SqlDB.SavePhoto(1, _data);
}
};
this.finish
来释放相机并触发onPause()
。因此,回调函数失去了它们的相机对象。我会暂时观察一下情况,如果问题确实解决了,我会标记为正确的。 - IAmGroot