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