在Firebase存储中存储多张图片

时间:2018-06-03 03:26:06

标签: java android firebase firebase-storage

我有一个用于在firebase存储中存储图像的代码,但它一次只存储一个图像,我需要存储多个图像 这是代码:

 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();
            }

        }
    });

}

代码看起来很长,但只是大致相同。我尝试重复这个方法取决于用户做什么,如果他只选择两个图像,它应该一次保存那两个,如果他选择树,它应该保存那些3等等...

1 个答案:

答案 0 :(得分:2)

您是否发现您的代码实际上是重复的?

将代码包装在这样的函数中:

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();
            }

        }
    });

}

现在使用可以直接使用它:

storeImage(imageUri);

或使用多个商店:

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

您可以使用代码存储任意数量的图像