简体   繁体   中英

Store Multiple pictures in Firebase Storage

I have a code for storing images in the firebase storage but it only storing one image at a time and I need to Store multiple images Here's the code:

 Save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Saveimages();
        }
    });

}

private void Saveimages() {

    comment = Comment.getText().toString();

    if (imageUri==null||imageUri2==null){
        Toast.makeText(this, "...Select a Image...", Toast.LENGTH_SHORT).show();
    }
    else if(imageUri!=null&&imageUri2!=null&&imageUri3==null&&imageUri4==null) {
        Save2ImagesFirebase();
    }
    else if(imageUri!=null&&imageUri2!=null&&imageUri3!=null&&imageUri4==null){
        Save3ImagesFirebase();
    }
    else if(imageUri!=null&&imageUri2!=null&&imageUri3!=null&&imageUri4!=null){
        Save4ImagesFirebase();
    }
}


private void Save2ImagesFirebase() {


    StorageReference filepath = mStorage.child(" Images");
    filepath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath2 = mStorage.child(" Images");
    filepath2.putFile(imageUri2).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

private void Save3ImagesFirebase() {

    StorageReference filepath = mStorage.child(" Images");
    filepath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath2 = mStorage.child(" Images");
    filepath2.putFile(imageUri2).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath3 = mStorage.child(" Images");
    filepath3.putFile(imageUri3).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

private void Save4ImagesFirebase() {

    StorageReference filepath = mStorage.child(" Images");
    filepath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath2 = mStorage.child(" Images");
    filepath2.putFile(imageUri2).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath3 = mStorage.child(" Images");
    filepath3.putFile(imageUri3).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

    StorageReference filepath4 = mStorage.child(" Images");
    filepath4.putFile(imageUri4).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

The code look long but is only much of the same. I try repeating the method depending on what the user do if he select only two images it should save those two at once and if he select tree it should save those 3 and so on...

Did you see that your code is actually duplicated?

Wrap your code in a function like this:

public void storeImage(Uri imageUri) {
    StorageReference filepath = mStorage.child(" Images");
    filepath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

            if (task.isSuccessful()){

                Toast.makeText(ImageVersus.this, " Published", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(ImageVersus.this, "..Error..", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

Now use can use it directly:

storeImage(imageUri);

or make use of multiple store:

public void storeMultipleImages(List<Uri> imageUris) {
    for (Uri uri : imageUris) {
        storeImage(uri);
    }
}

There you can store any number of images with the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM