【问题标题】:How can a wait function that uploads an image to firestore storage finish before running another function in android java?在android java中运行另一个函数之前,将图像上传到firestore存储的等待函数如何完成?
【发布时间】:2020-06-28 18:24:11
【问题描述】:

我想让应用程序将许多图像上传到 firestore 存储并获取这些图像的所有 url,并通过在 android 中使用 java 将其发布到 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(),"Upload Complete",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 = "fails " + 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没有等待上传图片完成。

【问题讨论】:

  • 你在 onSuccess() 中调用下一个。

标签: java android firebase asynchronous firebase-storage


【解决方案1】:

通过这样写上传图片解决了问题

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    相关资源
    最近更新 更多