-1

I am trying to capture an image from my app and then uploading it to firebase storage. I am trying to pass the captured image though intent. However, the onActivityResult() method is always showing null data.

This is for a note taking app that I am building. On clicking the camera icon from the app, the camera app of the phone should open and upon capturing, the resulting image should be uploaded to firebase. I am attaching my code for the same.

    private static final int Camera_Request_Code = 1;
protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 1
            );
        }

        mCaptureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, Camera_Request_Code);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Camera_Request_Code && resultCode == RESULT_OK) {


            mProgress.setMessage("Uploading image");
            mProgress.show();

            Log.d("...............",""+data.getData());
            Uri uri = data.getData();
            StorageReference filePath=mStorage.child("Images").child(uri.getLastPathSegment());
            filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    mProgress.dismiss();
                    Toast.makeText(CameraActivity.this,"Uploading finished",Toast.LENGTH_SHORT).show();
                }
            });
   }

    }

The error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mahima Tiwary
  • 183
  • 1
  • 2
  • 6
  • obviously `uri.getLastPathSegment()` is null – John Joe Apr 15 '19 at 17:24
  • Even data.getData() is showing null in the log. Hence uri is null. What is the solution? – Mahima Tiwary Apr 15 '19 at 17:35
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 15 '19 at 17:50

1 Answers1

0

The exception occurs because the Uri in onActivityResult() is null:

Uri uri = data.getData();

The documentation for capturing a camera image explains:

The Android Camera application saves a full-size photo if you give it a file to save into. You must provide a fully qualified file name where the camera app should save the photo.

Follow the example in the documentation to create a file Uri and add it to the intent for the camera app.

Community
  • 1
  • 1
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • No luck. Even after saving the captured image and passing the uri of the saved image as putExtra(), onActivityResult() is throwing nullpointer exception where I try to access the extra. – Mahima Tiwary Apr 15 '19 at 19:17
  • Thanks! I figured the error and now it is working perfectly fine – Mahima Tiwary Apr 15 '19 at 19:41