I am making an app to click photos and then upload the same on Firebase storage but I am getting this nullpointer
exception in my code.
I have tried bitmap image too but it didn't help as I am still getting the same error.
This is my code below of the onActivityResult
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_REQUEST_CODE&& resultCode==RESULT_OK){
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) {
Uri downloadUri = taskSnapshot.getUploadSessionUri();
Toast.makeText(MainActivity.this,"upload finished",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Sending failed", Toast.LENGTH_SHORT).show();
}
});
}
}
}
The error that I am getting:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference