0

I am trying to rename a folder in Firebase Storage and have no idea on how to do it. I have tried searching online where most of them say I have to get the file, write the file in the new path and delete the old file.

I tried this method

StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference sourceStorage = storageReference.child(userEmail + "/" + foodName + "/" + foodName + ".jpg");

StorageReference destinationStorage = storageReference.child(userEmail + "/" + userInput + "/" + userInput + ".jpg");

// Example:
// The path for sourceStorage is "abc@gmail.com/Rice/Rice.jpg"
// The path for destinationStorage is "abc@gmail.com/White Rice/White Rice.jpg"

sourceStorage.getDownloadUrl.addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                destinationStorage.putFile(uri);
                sourceStorage.delete():
            }
        });

I have tried the above code but it does not put the image into the destination path and the new path does not exist in firebase. Am I doing something wrong as I have no idea how to get the image to another folder if the user change the name. Thank you for your time.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Unknown
  • 3
  • 1
  • This `destinationStorage.putFile(uri)` tries to write a file in the new folder by passing a URL, which is not supported. You must first download the data to the application, and then write the data to the new location. For examples, see https://stackoverflow.com/questions/37686213/upload-image-from-url-to-firebase-storage/50459447#50459447 – Frank van Puffelen Feb 28 '21 at 16:36
  • How do I go about downloading the data temporary and upload it in firebase storage? I have seen the link but the code seems to be in another language which I am not familiar with at the moment. Thank you for your help. – Unknown Feb 28 '21 at 16:58
  • Ok I managed to did it by using `source.getBytes()` and `destination.putBytes(bytes)`. Do let me know if there is any other efficient way in doing this. Thank you for your help! – Unknown Feb 28 '21 at 17:52

0 Answers0