我是android新手。我正在开发一个简单的相机应用程序(使用互联网上的代码部分)。我希望它专注于单点触摸,并用两个手指触摸拍照。聚焦部分工作正常。但当我用两个手指触摸时,应用程序崩溃了。这是日志报告。
01-04 04:01:01.015: D/CamTest(8650): focused success
01-04 04:01:07.343: D/CamTest(8650): focused success
01-04 04:01:09.109: D/CamTest(8650): X0= 484.5 Y0=482.8125
01-04 04:01:09.109: D/CamTest(8650): X1= 269.5 Y1=229.39453
01-04 04:01:10.117: E/InputEventReceiver(8650): Exception dispatching input event.
01-04 04:01:10.117: E/MessageQueue-JNI(8650): Exception in MessageQueue callback: handleReceiveCallback
01-04 04:01:10.132: E/MessageQueue-JNI(8650): java.lang.RuntimeException: takePicture failed
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.hardware.Camera.native_takePicture(Native Method)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.hardware.Camera.takePicture(Camera.java:1061)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.hardware.Camera.takePicture(Camera.java:1006)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.example.camera_overlay.MainActivity$4.onTouch(MainActivity.java:65)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.View.dispatchTouchEvent(View.java:7122)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.View.dispatchPointerEvent(View.java:7307)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.os.MessageQueue.nativePollOnce(Native Method)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.os.MessageQueue.next(MessageQueue.java:125)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.os.Looper.loop(Looper.java:124)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at android.app.ActivityThread.main(ActivityThread.java:4812)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-04 04:01:10.132: E/MessageQueue-JNI(8650): at dalvik.system.NativeStart.main(Native Method)
01-04 04:01:10.132: D/AndroidRuntime(8650): Shutting down VM
01-04 04:01:10.132: W/dalvikvm(8650): threadid=1: thread exiting with uncaught exception (group=0x40bcd300)
01-04 04:01:10.164: E/AndroidRuntime(8650): FATAL EXCEPTION: main
01-04 04:01:10.164: E/AndroidRuntime(8650): java.lang.RuntimeException: takePicture failed
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.hardware.Camera.native_takePicture(Native Method)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.hardware.Camera.takePicture(Camera.java:1061)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.hardware.Camera.takePicture(Camera.java:1006)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.example.camera_overlay.MainActivity$4.onTouch(MainActivity.java:65)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.View.dispatchTouchEvent(View.java:7122)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.app.Activity.dispatchTouchEvent(Activity.java:2399)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.View.dispatchPointerEvent(View.java:7307)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.os.MessageQueue.nativePollOnce(Native Method)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.os.MessageQueue.next(MessageQueue.java:125)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.os.Looper.loop(Looper.java:124)
01-04 04:01:10.164: E/AndroidRuntime(8650): at android.app.ActivityThread.main(ActivityThread.java:4812)
01-04 04:01:10.164: E/AndroidRuntime(8650): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 04:01:10.164: E/AndroidRuntime(8650): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-04 04:01:10.164: E/AndroidRuntime(8650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-04 04:01:10.164: E/AndroidRuntime(8650): at dalvik.system.NativeStart.main(Native Method)
01-04 04:01:10.257: D/dalvikvm(8650): GC_CONCURRENT freed 301K, 5% free 9450K/9863K, paused 7ms+5ms, total 95ms
01-04 04:01:12.953: I/Process(8650): Sending signal. PID: 8650 SIG: 9
这是相关的代码部分(据我所知:/)。
preview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//get the Touch pointer count
int pointerCount = event.getPointerCount();
int action=event.getAction() & MotionEvent.ACTION_MASK;
// if two fingers, take the picture and get the coordinates
if(pointerCount==2){
if(action==MotionEvent.ACTION_POINTER_UP){
int pointerIndex0 = event.findPointerIndex(event.getPointerId(0));
// Get the 1st pointer's current position
float x0 = event.getX(pointerIndex0);
float y0 = event.getY(pointerIndex0);
Log.d(TAG, "X0= "+x0 + " Y0=" + y0);
int pointerIndex1 = event.findPointerIndex(event.getPointerId(1));
// Get the 2nd pointer's current position
float x1 = event.getX(pointerIndex1);
float y1 = event.getY(pointerIndex1);
Log.d(TAG, "X1= "+x1 + " Y1=" + y1);
if (safeToTakePicture) {
safeToTakePicture = false;
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
//preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
prvw=0;
}
//else, focus only
else if(pointerCount==1 && action==MotionEvent.ACTION_DOWN){
preview.camera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
if(success){
Log.d(TAG,"focused success");
}
}
});
}
return true;
}
});
Log.d(TAG, "onCreate'd");
}
@Override
public void onBackPressed()
{
if(prvw==1)
finish();
else
onCreate(null);
}
public void onResume(){
onCreate(null);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
//create the directory
String state = Environment.getExternalStorageDirectory().getPath()+"/CamOverlay";
File folder = new File(state);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if(!success)
Log.d(TAG,"directory creation failed");
// write to local sandbox file system
// outStream =
// System.currentTimeMillis()), 0);
// Or write to sdcard
outStream = new FileOutputStream(String.format(state+"/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
safeToTakePicture=true;
}
};
}
有人可以解释一下我的错误以及如何解决这个问题?谢谢你提前......:)
答案 0 :(得分:0)
我清除了这个.. :)如果有任何人有同样的问题我会指出我的错误。
我在单触事件上使用“向下触摸动作”,在multi_touch事件上使用“第二手指向上动作”。如果用户点击了双指,则相机会尝试同时执行此操作并且应用程序崩溃。