-1

我正在尝试使用自定义相机应用程序拍照,但不幸的是我正在调用NullPointerExceptionwhentakePhoto()方法。

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)
4

2 回答 2

0

好的,原来new ContentValues()是空的,所以我尝试用一​​些数据来初始化它:

ContentValues values=new ContentValues();
values.put(MediaStore.MediaColumns.DATA, data);
values.put(MediaStore.MediaColumns.TITLE, "abc");
values.put(MediaStore.MediaColumns.SIZE, data.length);
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
于 2012-08-06T15:56:26.753 回答
-1

尝试这个..

 button.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {

                 takePhoto();
            }   

        });


 private void takePhoto(){
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 

    }



protected void onActivityResult(int requestCode, int resultcode, Intent intent)
  {
      super.onActivityResult(requestCode, resultcode, intent);

  if (requestCode == CAMERA_REQUEST) {  
      Bitmap photo = (Bitmap) intent.getExtras().get("data"); 
      }  

      else
      {
          Log.d("Status:", "Photopicker canceled");           
      }
  }
于 2012-07-31T11:45:55.050 回答