How can a wait function that uploads an image to firestore storage finish before running another function in android java?

huangapple go评论73阅读模式
英文:

How can a wait function that uploads an image to firestore storage finish before running another function in android java?

问题

以下是已翻译的内容:

我想要制作一个应用程序,通过在Android中使用Java,将许多图像上传到Firestore存储,并获取所有这些图像的URL,并将其发布到Firestore数据库中。

我有这个用于在数据库中发布的函数:

public void uploadFile(Map<String, Object> item, String phone) {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("Users").document().collection(phone).add(item)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Toast.makeText(getApplicationContext(), "上传完成", Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}

数据是我要发布到数据库中的数据哈希表。此数据包含以下信息:

Map<String, Object> data = new HashMap<>();
data.put("name", name);
data.put("phone", phone);
data.put("city", city);
data.put("itemName", itemName);
data.put("price", price);
data.put("type", type);
data.put("subtype", subtype);
data.put("cond", cond);
data.put("details", details);

我有这个用于上传照片的函数:

public void upLoadImage(File data, Map<String, Object> list, int KEY) {
    Uri file = Uri.fromFile(data);

    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReference().child("images/" + file.getLastPathSegment());
    UploadTask uploadTask = storageRef.putFile(file);
    
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            url = "失败:" + e.getMessage();
        }
    });
    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    if (KEY == 1) {
                        list.put("image1", String.valueOf(uri));
                    } else if (KEY == 2) {
                        list.put("image2", String.valueOf(uri));
                    } else if (KEY == 3) {
                        list.put("image3", String.valueOf(uri));
                    }
                }
            });
        }
    });
}

此函数接受两个参数:

  1. 一个列表,将下载URL推送到其中。
  2. 图像文件。

我想要像这样运行它:

uploadImage(path1, data, 1);
uploadImage(path2, data, 2);
uploadImage(path3, data, 3);
uploadFile();

这意味着我想要上传图像并等待上传完成,然后上传第二个图像并等待上传完成,依此类推,最后运行uploadFile(),因为我的问题是uploadFile()不会等待图像上传完成。

英文:

I want to make app upload many images to firestore storage and get all of the urls of those images and post it in firestore database by using java in android.

I have this function to post on database

  public void uploadFile(Map&lt;String,Object&gt; item , String phone){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection(&quot;Users&quot;).document().collection(phone).add(item)
            .addOnSuccessListener(new OnSuccessListener&lt;DocumentReference&gt;() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Toast.makeText(getApplicationContext(),&quot;Upload Complete&quot;,Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });

}

The data is a hashmap of data I will post to the database. This data contains this information:

 Map&lt;String,Object&gt; data = new HashMap&lt;&gt;();
                      data.put(&quot;name&quot; , name);
                      data.put(&quot;phone&quot; , phone);
                      data.put(&quot;city&quot; , city);
                      data.put(&quot;itemName&quot; , itemName);
                      data.put(&quot;price&quot; , price);
                      data.put(&quot;type&quot; , type);
                      data.put(&quot;subtype&quot; , subtype);
                      data.put(&quot;cond&quot; , cond);
                      data.put(&quot;details&quot; , details);

and I have this function to upload photo

public void upLoadImage(File data,Map&lt;String,Object&gt; list,int KEY){

   Uri file = Uri.fromFile(data);

   FirebaseStorage storage = FirebaseStorage.getInstance();
   StorageReference storageRef = storage.getReference().child(&quot;images/&quot;+file.getLastPathSegment());
   UploadTask uploadTask = storageRef.putFile(file);
   
   uploadTask.addOnFailureListener(new OnFailureListener() {
       @Override
       public void onFailure(@NonNull Exception e) {
           url = &quot;fails &quot; + e.getMessage();

       }
   });
   uploadTask.addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
       @Override
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

          storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
              @Override
              public void onSuccess(Uri uri) {

                      if(KEY == 1){
                          list.put(&quot;image1&quot;,String.valueOf(uri));
                      }else if(KEY ==2){
                          list.put(&quot;image2&quot;,String.valueOf(uri));
                      }else if(KEY == 3){
                          list.put(&quot;image3&quot;,String.valueOf(uri));
                      }
              }
          });

       }
   });

this function take two argument data:

  1. list that will push download url inside it
  2. image file

I want to run it just like this

uploadImage(path1,data,1);
uploadImage(path2,data,2);
uploadImage(path3,data,3);
uploadfile()

This means that I want to upload image and complete then upload sec image and complete etc., then run uploadfile because my problem that uploadfile not wait upload image to complete.

答案1

得分: 0

以下是翻译好的部分:

public void upLoadImage(String path, String path2, String path3, Map<String, Object> list, int KEY, String Auth) {

    if (!path.equals("empty") && path != null) {

        Toast.makeText(getApplicationContext(), "path = " + path, Toast.LENGTH_LONG).show();
        Uri file = Uri.fromFile(new File(path));
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageRef = storage.getReference().child("images/" + file.getLastPathSegment());
        UploadTask uploadTask = storageRef.putFile(file);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "No Connection", Toast.LENGTH_LONG).show();
            }
        });
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        if (KEY == 1) {
                            list.put("image1", String.valueOf(uri));
                            upLoadImage(path2, "null", path3, list, 2, Auth);
                        }
                        if (KEY == 2) {
                            list.put("image2", String.valueOf(uri));
                            upLoadImage(path3, "null", "null", list, 3, Auth);
                        }
                        if (KEY == 3) {
                            list.put("image3", String.valueOf(uri));
                            uploadFile(list, Auth);
                        }
                    }
                });

            }
        });
    } else {
        uploadFile(list, Auth);
    }
}
英文:

The problem was resolved by write upload image like this

public void upLoadImage(String path,String path2, String path3,Map&lt;String,Object&gt; list,int KEY,String Auth){
if(!path.equals(&quot;empty&quot;) &amp;&amp; path != null){
Toast.makeText(getApplicationContext(),&quot;path = &quot; + path,Toast.LENGTH_LONG).show();
Uri file = Uri.fromFile(new File(path));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child(&quot;images/&quot;+file.getLastPathSegment());
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),&quot;No Connection&quot;,Toast.LENGTH_LONG).show();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
@Override
public void onSuccess(Uri uri) {
if(KEY == 1 ){
list.put(&quot;image1&quot;,String.valueOf(uri));
upLoadImage(path2,&quot;null&quot;,path3,list,2,Auth);
}
if(KEY == 2){
list.put(&quot;image2&quot;,String.valueOf(uri));
upLoadImage(path3,&quot;null&quot;,&quot;null&quot;,list,3,Auth);
}
if(KEY == 3){
list.put(&quot;image3&quot;,String.valueOf(uri));
uploadFile(list,Auth);
}
}
});
}
});
}else {
uploadFile(list,Auth);
}
}

huangapple
  • 本文由 发表于 2020年6月29日 02:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/62626577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定