Skip to content Skip to sidebar Skip to footer

Upload Multiple Images On Firebase - Android Studio

Hi guys im trying to upload 3 images on firebase with captions but it only uploads the first successfully then the field for image 2 and 3 it copies the download url for image 1 on

Solution 1:

That's because you're only calling putFile() with the first image. Which means the first image will be the only one uploaded. You need to call putFile() on the other images as well:

StorageReferencefilepath= mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
    StorageReferencefilepath1= mStorage.child("Blog_Images").child(mImageUri1.getLastPathSegment());
    StorageReferencefilepath2= mStorage.child("Blog_Images").child(mImageUri2.getLastPathSegment());
    finalDatabaseReferencenewPost= mDatabase.push();

    filepath.putFile(mImageUri).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

        @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            Uridownloadurl= taskSnapshot.getDownloadUrl();
            newPost.child("title").setValue(title_val);
            newPost.child("desc").setValue(desc_val);
            newPost.child("number").setValue(namba_val);
            newPost.child("passport").setValue(pass_val);
            newPost.child("info").setValue(info_val);
            newPost.child("steps").setValue(steps_val);
            newPost.child("police").setValue(police_val);
            newPost.child("image").setValue(downloadurl.toString());
        }
    });
    filepath1.putFile(mImageUri1).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

        @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Uridownloadurl= taskSnapshot.getDownloadUrl();
            newPost.child("image1").setValue(downloadurl.toString());
        }
    });
    filepath2.putFile(mImageUri2).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

        @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            Uridownloadurl= taskSnapshot.getDownloadUrl();
            newPost.child("image2").setValue(downloadurl.toString());
            mProgress.dismiss();
            finish();
        }
    });

Solution 2:

You are only saving image to

filepath.putFile(mImageUri).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

            @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Uridownloadurl= taskSnapshot.getDownloadUrl();

                DatabaseReferencenewPost= mDatabase.push();
                newPost.child("title").setValue(title_val);
                newPost.child("desc").setValue(desc_val);

                newPost.child("number").setValue(namba_val);
                newPost.child("passport").setValue(pass_val);
                newPost.child("info").setValue(info_val);
                newPost.child("steps").setValue(steps_val);
                newPost.child("police").setValue(police_val);

                newPost.child("image").setValue(downloadurl.toString());
                newPost.child("image1").setValue(downloadurl.toString());
                newPost.child("image2").setValue(downloadurl.toString());


                mProgress.dismiss();

                finish();



            }
        });

Use same function for uploading other imagestoo ..

filepath1.putFile(mImageUri1).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

            @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Uridownloadurl= taskSnapshot.getDownloadUrl();

                DatabaseReferencenewPost= mDatabase.push();
                newPost.child("title").setValue(title_val);
                newPost.child("desc").setValue(desc_val);

                newPost.child("number").setValue(namba_val);
                newPost.child("passport").setValue(pass_val);
                newPost.child("info").setValue(info_val);
                newPost.child("steps").setValue(steps_val);
                newPost.child("police").setValue(police_val);

                newPost.child("image").setValue(downloadurl.toString());
                newPost.child("image1").setValue(downloadurl.toString());
                newPost.child("image2").setValue(downloadurl.toString());


                mProgress.dismiss();

                finish();



            }
        });

filepath2.putFile(mImageUri2).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {

            @OverridepublicvoidonSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Uridownloadurl= taskSnapshot.getDownloadUrl();

                DatabaseReferencenewPost= mDatabase.push();
                newPost.child("title").setValue(title_val);
                newPost.child("desc").setValue(desc_val);

                newPost.child("number").setValue(namba_val);
                newPost.child("passport").setValue(pass_val);
                newPost.child("info").setValue(info_val);
                newPost.child("steps").setValue(steps_val);
                newPost.child("police").setValue(police_val);

                newPost.child("image").setValue(downloadurl.toString());
                newPost.child("image1").setValue(downloadurl.toString());
                newPost.child("image2").setValue(downloadurl.toString());


                mProgress.dismiss();

                finish();



            }
        });

Solution 3:

Your filepath is always the same for your photos, that's why you are uploading only the fist one. So to solve this, you need to use different paths of each photo like this:

filepath1.putFile(mImageUri1).addOnSuccessListener(/* ... */);
filepath2.putFile(mImageUri2).addOnSuccessListener(/* ... */);
filepath3.putFile(mImageUri3).addOnSuccessListener(/* ... */);

Post a Comment for "Upload Multiple Images On Firebase - Android Studio"