Jack Sparrow
Jack Sparrow

Reputation: 95

How to upload 2 images on firebase storage on click with URL in firestore

So I want to upload cover photo if user chosen or profile image if chosen on save btn clicked here is my UI design for better understanding.

image desc

for choosing image from gallery I did something like this:

    private void chooseImage(int imgReqCode) {
    // Defining Implicit Intent to mobile gallery
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(
            Intent.createChooser(
                    intent,
                    "Select Image from here..."),
            imgReqCode);
}

and this is onActivityResult code:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    checkAndSetImage(requestCode, COVER_IMG_REQUEST, resultCode, data, binding.coverPhoto, coverUri);
    checkAndSetImage(requestCode, PROFILE_IMG_REQUEST, resultCode, data, binding.profilePhoto, profileUri);
}

And this is my checkAndSetImage method :

private void checkAndSetImage(int requestCode, int PICK_IMAGE_REQUEST, int resultCode, Intent data, ImageView imageView, Uri filePath) {
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        // Get the Uri of data
        filePath = data.getData();
        try {
            // Setting image on image view using Bitmap
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            // Log the exception
            e.printStackTrace();
        }
    }

}

And this is my Uri 's which is declare as global :

private Uri coverUri, profileUri;

After that I am confused How can I upload that to firebase storage and get both or which is available's url in my firestore database

or According to you how can you upload 2 Images (1. Cover, 2. Profile) in firebase database accoriding to my layout

image desc according to my layout

Upvotes: 2

Views: 243

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138999

There is no way you can upload the coverUri and the profileUri pictures in parallel and expect to complete the upload at the same time. What you have to do is to create two separate uploads, and once the upload URL is available, write it to Firestore. Please check below how you can get the download URL in Java:

Upvotes: 2

Related Questions