0

EDIT: The discussion on this issue has been carried to another question: Image capture with camera & upload to Firebase (Uri in onActivityResult() is null) PLEASE check it out! I did some manipulations (following the android documentation) but still getting same problem.

So here I'm trying to do a simple task of capturing an image with camera and uploading it to my storage database on Firebase. I have the following button to do the job:

b_capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_REQUEST_CODE);
        }
    });

and the following code for the upload:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
        progressDialog.setMessage("Uploading...");
        progressDialog.show();
        Uri uri = data.getData();
        StorageReference filepath = storage.child("Photos").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(MainActivity.this, "Upload Successful!", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(MainActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show();
            }
        });
      }
    }

in the app I'm able to take picture, but after I confirm the picture taken the APP CRASHES and I get the following error:

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

So, I understand that the "Uri" does not have anything in it, but I'm confused about what can be done. Need help.

And yes, I've checked all permissions, Firebase connections, Gradle sync etc.

Community
  • 1
  • 1
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • See this answer: http://stackoverflow.com/a/40567453/4815718 – Bob Snyder Nov 21 '16 at 00:34
  • @qbix Sorry; wrong question(my previous answer). I have already seen that question and did just as it said - still it doesn't work. The discussion following that path is carried to another question now: http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul – Sergey Emeliyanov Nov 21 '16 at 00:36
  • @qbix Please check the question: http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul I've already done this, it still does't work for me. Please help! – Sergey Emeliyanov Nov 21 '16 at 00:43

2 Answers2

1

You need to re-read the documentation and follow the example to create a file and store its Uri in the image capture intent. This is the code from the documentation you need to add:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}


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

            //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //startActivityForResult(intent, CAMERA_REQUEST_CODE);
        }
    });
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I've already answered you in the comments that I've already done exactly what you have written. If you'd like to help please check http://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul - here I've already done that part from the documentation. EDIT: So yea, we've found the mistake in the discussion I've linked you to; and now the code works. Since your answer is sort of a correct one (even though it didn't help solving the question) I'm giving you the credit for your effort. Thank you! – Sergey Emeliyanov Nov 21 '16 at 07:51
0

Question solved in the following discussion: Image capture with camera & upload to Firebase (Uri in onActivityResult() is null) Thanks to everyone who participated!

Community
  • 1
  • 1
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52