-1

Unable to extract text from image after capturing the image through camera.In logcat i'm getting some exceptions (from CropImage.activity(data.getData())).Below is the logcat with some exceptions and java code.

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 code for the camera

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

            }
mohammed nabil
  • 95
  • 2
  • 13
  • `onActivityResult()` could be called as a result of some other `startActivityForResult()` call. The `requestCode` can help you ensure your call to `data.getData()` only happens when `startActivityForResult()` is called from `pickCamera()`. To see how, check out https://stackoverflow.com/a/14148838/2848676. If some other intent is causing `onActivityResult()` to be called, a `null` value of `data` could valid in that case. So you want to filter it out using `requestCode`. I'm not sure that will solve your problem, but it could at least rule out one possible cause. – Michael Osofsky Jan 10 '19 at 19:29
  • 1
    Possible duplicate of [Android Camera : data intent returns null](https://stackoverflow.com/questions/9890757/android-camera-data-intent-returns-null) – Ali Ahsan Jan 10 '19 at 21:49

1 Answers1

0

The default Android camera application returns a non-null intent only when passing back a thumbnail in the returned Intent. If you pass EXTRA_OUTPUT with a URI to write to, it will return a null intent and the picture is in the URI that you passed in

for more detail refer to this link

Ali Ahsan
  • 985
  • 11
  • 16