我正在尝试使用自定义相机应用程序拍照,但不幸的是我正在调用NullPointerException
whentakePhoto()
方法。
onPictureTaken()
在这一行的回调中抛出异常:
Uri imageUri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
但是,现在我得到了另一个 NPE:
07-31 14:59:38.117: W/System.err(22224): java.lang.NullPointerException
07-31 14:59:38.156: W/System.err(22224): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:467)
07-31 14:59:38.156: W/System.err(22224): at android.content.ContentResolver.openOutputStream(ContentResolver.java:363)
07-31 14:59:38.156: W/System.err(22224): at android.content.ContentResolver.openOutputStream(ContentResolver.java:339)
07-31 14:59:38.156: W/System.err(22224): at com.testcamera.TestCameraActivity.onPictureTaken(TestCameraActivity.java:154)
这是我设置侦听器的方式:
@Override
public void onClick(View v) {
camera.takePicture(null, null, this);
}
这是onPictureTaken
回调:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Uri imageUri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
try{
OutputStream imageOS=getContentResolver().openOutputStream(imageUri);
imageOS.write(data);
imageOS.flush();
imageOS.close();
}catch (Exception e) {
e.printStackTrace();
}
camera.startPreview();
}
LogCat 详细信息:
07-31 14:05:46.409: E/AndroidRuntime(2242): java.lang.NullPointerException
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.os.Parcel.readException(Parcel.java:1328)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.content.ContentResolver.insert(ContentResolver.java:604)
07-31 14:05:46.409: E/AndroidRuntime(2242): at com.testcamera.TestCameraActivity.onPictureTaken(TestCameraActivity.java:131)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:571)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.os.Looper.loop(Looper.java:130)
07-31 14:05:46.409: E/AndroidRuntime(2242): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-31 14:05:46.409: E/AndroidRuntime(2242): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 14:05:46.409: E/AndroidRuntime(2242): at java.lang.reflect.Method.invoke(Method.java:507)
07-31 14:05:46.409: E/AndroidRuntime(2242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-31 14:05:46.409: E/AndroidRuntime(2242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-31 14:05:46.409: E/AndroidRuntime(2242): at dalvik.system.NativeStart.main(Native Method)