gpt4 book ai didi

java - 在 Firebase 存储中存储多张图片

转载 作者:行者123 更新时间:2023-11-29 04:18:36 25 4
gpt4 key购买 nike

我有一个用于在 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 个,依此类推......

最佳答案

您是否看到您的代码实际上是重复的?

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

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

在那里你可以用代码存储任意数量的图像

关于java - 在 Firebase 存储中存储多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com