1
votes

This is the my code ( check this file full code on GitHub) for Uploading an image to Firebase Storage.

// ImagePickerButton shows an image picker to upload a image for a message
    mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            i.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
            startActivityForResult(Intent.createChooser(i, "Complete action using"), RC_PHOTO_PICKER);
        }
    });

This is the OnActivityResult Method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {

      Uri selectedImageUri = data.getData();

      //get the reference to stored file at database
      StorageReference selectedImageFilePath = mStorageReference.child(selectedImageUri.getLastPathSegment());

      //upload file to firebase
      selectedImageFilePath.putFile(selectedImageUri).addOnSuccessListener(MainActivity.this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
          @SuppressWarnings("VisibleForTests")
          String downloadUrl = taskSnapshot.getDownloadUrl().toString();
          Message message = new Message(null, userName, downloadUrl);
          messagesDatabaseReference.push().setValue(message);
        }
      });
    }
  }
}

Picking an Image from the Gallery works fine but I'm not able to upload the selected Image on Firebase Storage. Correct my code If I'm wrong at any site. However, I manually entered these lines in my manifest file

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I'm currently running the app in my own mobile device connected via USB( Moto G4 - Nougat)

4
Does it count? Because, Firebase by default runs with Internet permissions right? @PatrixWilliams - coderpc

4 Answers

0
votes

data.getData() could return an URI that does not point to a File.

You should use InputStream is = getContentResolver().openInputStream(data.getData()) and than use selectedImageFilePath.putStream(is)

Also you could add an onFailureListener to the upload task and debug what is going wrong.

In addition, if I recall correctly, I do not think you need the storage permissions when you directly read the data in onActivityResult.

0
votes

Try this for button click :

//Select Image functionality
    btnselectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 2);
        }
    });

For saving image to fire base :

//Storing and getting image from firebase
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

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

        final Uri uri = data.getData();

        StorageReference filepath = mStorageRef.child("Photos").child(name);
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Uri downloadUri = taskSnapshot.getDownloadUrl();
                //Getting url where image is stored
                saveImage(downloadUri);
                Picasso.with(getActivity()).load(downloadUri).fit().centerCrop().into(profileImage);
            }
        });
    }

This works fine for me, Hope this will help.

0
votes

Instead of else if statement, I modified it by writing a seperate if statement.

if (requestCode == RC_SIGN_IN) {
  if (resultCode == RESULT_OK) {

  }else if (resultCode == RESULT_CANCELED) {

   }

  // This was the change I made
  if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {

  }
}
-1
votes
If ( //sign in)
{
       If (//result ok)
           {}
       else if (//result cancel)
          {}
}

else if (//rc photot picker && result ok)
   { //your storage code here.....}

tats works perfectly.....check out the braces carefully....