Home > Blockchain >  firebase how to download image and update it again?
firebase how to download image and update it again?

Time:12-06

my image is stored inside firebase storage and its url is stored inside the realtime database. Firebase realtime and storage are both like this:

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:

    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)...

CodePudding user response:

You have to download the bytes from firebase like this:

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

                                
  • Related