java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.net.Uri android.content.Intent.getData()”

时间:2019-01-10 19:18:25

标签: android android-studio

通过相机捕获图像后无法从图像中提取文本。在logcat中,我遇到了一些异常(来自CropImage.activity(data.getData()))。下面是具有一些异常和Java代码的logcat。 / p>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
        at com.example.nabil.textocr.MainActivity.onActivityResult(MainActivity.java:172)
  

相机的Java代码

dialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which==0){
                   if (!checkCameraPermission()){
                       requestCameraPermission();
                   }else {
                       pickCamera();
                   }
                }

 private void pickCamera() {
        ContentValues values=new ContentValues();
        values.put(MediaStore.Images.Media.TITLE,"NewPic");
        values.put(MediaStore.Images.Media.DESCRIPTION,"Image to text");
        image_uri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
        Intent cameraintent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
        startActivityForResult(cameraintent,IMAGE_PICK_GALLERY_CODE);
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode==RESULT_OK){
            if (requestCode==IMAGE_PICK_GALLERY_CODE){
                CropImage.activity(data.getData())
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .start(this);

            }

1 个答案:

答案 0 :(得分:0)

仅当在返回的Intent中传回缩略图时,默认的Android相机应用程序才返回非null的Intent。如果您将EXTRA_OUTPUT与要写入的URI一起传递,它将返回空意图,并且图片位于您所传递的URI中

有关更多详细信息,请参阅此link