简体   繁体   English

firebase 如何下载镜像并再次更新?

[英]firebase how to download image and update it again?

my image is stored inside firebase storage and its url is stored inside the realtime database.我的图像存储在 firebase 存储中,其 url 存储在实时数据库中。 Firebase realtime and storage are both like this: Firebase 实时和存储都是这样的:

Type1
----Time1
--------elements of type1
----Time2
--------elements of type2
Type2
....

I let the users change element's time so I have to move an element from a branch to another.我让用户更改元素的时间,所以我必须将一个元素从一个分支移动到另一个。 My strategy is to create a new element on the new branch and then remove the old one.我的策略是在新分支上创建一个新元素,然后删除旧元素。 The problem is in the creation of the new element in the chosen branch, specifically I have problems setting the image of the new element equal to that of the old one.问题在于在所选分支中创建新元素,特别是我在将新元素的图像设置为与旧元素的图像相同时遇到了问题。 I tried to use Uri.parse() passing the url saved as string in the realtime database and I also tried to use storagereference.getdownloadurl, both I got this error:我尝试使用 Uri.parse() 传递在实时数据库中保存为字符串的 url,我也尝试使用 storagereference.getdownloadurl,我都收到了这个错误:

    could not locate file for uploading:https://firebasestorage.googleapis.com/v0/b/re...
E/StorageException: StorageException has occurred.
    An unknown error occurred, please check the HTTP result code and inner exception for server response.
     Code: -13000 HttpResult: 0

And it's not sense because If I click that link in the error, it shows me the correct image, so why it can't locate it??这没有意义,因为如果我单击错误中的那个链接,它会显示正确的图像,那么为什么它找不到它呢? How can I do it?我该怎么做?

storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/General/Test1");
    storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/Future/Test1");
            storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ...
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
        }
    });

The error is on this line:错误在这一行:

storageReference.putFile(uri)...

You have to download the bytes from firebase like this:您必须像这样从 firebase 下载字节:

storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/General/Test1");
storageReference.getBytes(1024*1024).addOnSuccessListener(new OnSuccessListener<byte[]>() {
    @Override
    public void onSuccess(byte[] bytes) {
       //here add new element in the server with storageReference.putbytes(bytes)..
    }
});

                                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM