Android - 使用自定义相机应用拍照时应用程序崩溃了

时间:2012-07-31 11:19:03

标签: android android-layout android-intent android-emulator android-widget

我尝试使用自定义相机应用拍摄照片,但不幸的是,我在调用NullPointerException方法时获得takePhoto()

此行的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)

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:-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");           
      }
  }