在onSuccessListener中成功上传文件后,如何返回布尔值? [英] How to return a boolean value when a file is successfully uploaded inside onSuccessListener?

查看:83
本文介绍了在onSuccessListener中成功上传文件后,如何返回布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先看这个.我在 submitProfile()

public void submitProfile() {
     if(!uploadUserPdf()) {
          return;
     }
     else {
        //...............
     }
}

这是 uploadUserPdf()函数

private boolean uploadUserBiodataPdf() {
        Uri fileUri = Uri.parse(Pdf);
        final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Pictures");
        StorageReference pdfRef = storageReference.child(mUser.getUid()).child(fileUri.getLastPathSegment());

        pdfRef.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                pdfRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Pdf = uri.toString();
                        Toast.makeText(ContinueProfile.this, "Pdf Uploaded", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
        return false;
    }

如果文件上传成功,我想返回true .但是我找不到写 return true 的空间.如果文件成功上传,是否可以返回true值?

I want to return true if file is successfully uploaded. But I cannot find any space to write return true. Is it possible to return the true value if file is successfully uploaded ??

推荐答案

将文件上传到Firebase存储是异步进行的,因为这可能会花费一些时间.当上传在后台进行时,您的主要代码继续在前台执行.然后,当上传完成后,您的 onSuccess 就会被调用.

Uploading the file to Firebase Storage happens asynchronously, since it may take time. While the uploading is going on in the background, your main code continues to execute in the foreground. Then when the upload completes, your onSuccess is called.

这意味着在您的 return false 运行时,尚未调用任何 onSuccess 方法.而且您无法返回尚未发生的现在.

What this means is that by the time your return false runs, neither of your onSuccess methods have been called. And you can't return something now that hasn't happened yet.

由于这个原因,任何返回下载URL的代码都必须位于 onSuccess 方法中,该方法在从服务器中检索到下载URL时会被调用.就像您的 Toast (在适当的时间运行)一样.

For this reason any code that requites the download URL needs to be inside the onSuccess method that gets called when that download URL has been retrieved from the server. Like your Toast, which runs at the right time.

如果要将使用下载URL的代码与生成它的代码分开,则可以传入自定义回调接口,然后在获得下载URL时调用它:

If you want to keep the code that uses the download URL separate from the code that generates it, you can pass in a custom callback interface, and then call that when you get the download URL:

public interface GetDownloadUrlCallback {
  void onCallback(String uri);
}
private boolean uploadUserBiodataPdf(GetDownloadUrlCallback callback) {
    Uri fileUri = Uri.parse(Pdf);
    final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Pictures");
    StorageReference pdfRef = storageReference.child(mUser.getUid()).child(fileUri.getLastPathSegment());

    pdfRef.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            pdfRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    callback(uri.toString());
                }
            });
        }
    });

然后调用:

GetDownloadUrlCallback(new GetDownloadUrlCallback() {
  @Override
  public void onCallback(String uri) {
    Log.i("GetDownloadUrlCallback", "Got uri: "+uri);
  }
});

另请参阅:

这篇关于在onSuccessListener中成功上传文件后,如何返回布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆