使上传方法返回String“已上传文件的网址". [英] Make upload method return String "the uploaded file url"

查看:59
本文介绍了使上传方法返回String“已上传文件的网址".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像上传到Firebase存储,我需要存储中的图像URL才能使用它,然后将其插入FireStore中的文档中. 如何在上传过程完成之后而不是在完成之前获取网址?

I'm uploading an image to Firebase Storage, I need the image URL in the Storage to use it then to insert it within a document in FireStore. How to get the url after the upload process is finished and not before it finished?

   public String uploadImage(byte[] bytes) {
        try {
            final StorageReference ref = storage.child("images/" + new Date().toString());
            ref.putBytes(bytes)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    res = uri.toString();
                                    return;
                                }
                            });
                        }
                    });
            System.out.println("RES : " + res);
            return res;
        }catch (NullPointerException e){
            return null;
        }
    }

//the variable res must return a not null value.

推荐答案

参考::

这篇关于使上传方法返回String“已上传文件的网址".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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