1
votes

I'm trying to upload an image from Gallery to Firebase Storage.

Picking an Image from the Gallery works fine but then the selected Image is not being uploaded to Firebase Storage.

I think the problem is with this module in my code which I'm not able to figure out.

if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {

  Uri selectedImageUri = data.getData();

  // Get a reference to store file at photos/<FILENAME>
  StorageReference photoRef = mPhotosStorageReference.child(selectedImageUri.getLastPathSegment());

  //Upload file to Firebase Storage
  photoRef.putFile(selectedImageUri)
    .addOnSuccessListener(this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
      @Override
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

        // When the image has successfully uploaded, we get its download URL
        @SuppressWarnings("VisibleForTests")
        Uri downloadUrl = taskSnapshot.getDownloadUrl();

        // Set the download URL to the message box, so that the user can send it to the database
        Message message = new Message(null, userName, downloadUrl.toString());
        messagesDatabaseReference.push().setValue(message);
      }
    });

I'm running my app on my mobile device Moto G4 (Android Nougat) through USB.

1

1 Answers

0
votes

In my OnActivityResult, I modified the following:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == RC_SIGN_IN) {
    if (resultCode == RESULT_OK) {
      ....
    } else if (resultCode == RESULT_CANCELED) {
      ....
    }
  } else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {

  }
}