将文件上传到Firebase后如何获取文件URL? [英] How to get file URL after uploading them to Firebase?

查看:115
本文介绍了将文件上传到Firebase后如何获取文件URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件上传到Firebase后,如何获取其URL,以便将其存储以备后用?我想将该URL写入Firebase数据库,以便其他用户可以访问该图像.

Once you have uploaded a file to Firebase how can you get it's URL so that you can store that for later use? I want to write the URL to a Firebase Database so that other users can access the image.

我正在这样上传文件:

public void uploadFile()
{

    StorageReference filepath = mstorageRef.child("folder").child(filename);

    Uri File= Uri.fromFile(new File(mFileName));

    filepath.putFile(File).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
        {
            Toast.makeText(MtActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
        }
    });
}

我已经确认文件实际上正在上传,所以现在我只需要可以写入数据库的URL.但是,当我尝试这样做时:

I have confirmed that the files are in fact uploading so now I just need the URL which I can write to my Database. However when I tried to do this:

Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();

它给我一个错误,并说This method should only be accessed from tests or within private scope

It gives me an error and says This method should only be accessed from tests or within private scope

我不确定这是什么意思,我也不知道为什么会出现该错误,因为我正在关注

I'm not sure what that means and I also don't know why I would be getting that error since I'm following this example provided by Firebase.

是否有获取网址的新方法?

Is there a new way of getting the URL?

而且,该URL特别对于该商品来说唯一吗?意思是,如果我将其存储到数据库中并稍后尝试访问它,我将能够这样做吗?

Also, is that URL unique to that item in particular? Meaning if I store it to a database and try to access it later will I be able to do so?

推荐答案

您正在使用不推荐使用的UploadTask.getDownloadUrl().您可以使用 StorageReference.getDownloadUrl().

You're using UploadTask.getDownloadUrl() which is deprecated. You can use StorageReference.getDownloadUrl().

根据您的情况,您可以尝试为-

In your case you can try this as -

 filepath.putFile(File).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
    {
         filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Uri downloadUrl = uri;
                       //Do what you want with the url
                    }
        Toast.makeText(MtActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
    }
});

请注意StorageReference.getDownloadUrl()返回Task,该任务必须异步处理,您不能执行Uri downloadUrl = photoRef.getDownloadUrl().getResult();,否则会得到java.lang.IllegalStateException: Task is not yet complete

Take care that StorageReference.getDownloadUrl() returns Task, which must be handled asynchronously, you cannot do Uri downloadUrl = photoRef.getDownloadUrl().getResult(); else you will get java.lang.IllegalStateException: Task is not yet complete

这篇关于将文件上传到Firebase后如何获取文件URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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