gryzek gryzek - 1 month ago 37
Android Question

OnActivityResult: Uri and getData is a null object during capture a photo

I have a problem with Uri object. User should take a photo and next this is sending to Firebase Storage. But sth is wrong with onActivityResult.

I read a lot of topics (https://developer.android.com/training/camera/photobasics.html, StackOverFlow too) but nothing works with this code.

There is an error:

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

Below is a code:

mUploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {

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 && data != null) {

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

Uri uri = data.getData();

StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());


filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

mProgress.dismiss();

Uri downloadUri = taskSnapshot.getDownloadUrl();

Picasso.with(MainActivity.this).load(downloadUri).fit().centerCrop().into(mImageView);

Toast.makeText(MainActivity.this, "Upload Done.", Toast.LENGTH_LONG).show();
}
});

}
}
}


As I checked, data (Intent) in onActivityResult is equal to NULL, so uri is null as well.

So, how can I resolve this challenge and make it usable? Should I use a Bitmap to have an access to a photo?

Could someone help me to resolve this situation?

Regards

Answer

Always create your file and store the captured image using the path as follows

File photoFile = null;     

mUploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  if (intent.resolveActivity(getPackageManager()) != null) {  

        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
           }
         }
     });
}

private File createImageFile() throws IOException {
    // Create an image mSelectedFile name
    String timeStamp = new SimpleDateFormat(Constants.PHOTO_DATE_FORMAT, Locale.ENGLISH).format(new Date());
    String imageFileName = "IMG_" + timeStamp;
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );


    return file;
}

Then use this file "photoFile" and use it as needed in onActivityResult.

Comments