简体   繁体   中英

Rename picture before uploading to Firebase Storage

The selected picture from the user is selectedImage . My program uploads a user's picture under his unique nickname folder. Eg if david uploads a pic, it goes under "david" folder, but I want to be able to have the same filename for every picture uploaded. The goal: If david uploads 1 pic and then another pic, the 2nd one will replace the 1st one because they both have the same file name. I tried doing renameTo but instead it uploads under a new folder under david/imagename/old.jpg

I want it to be like this david/pic.jpg that's it. None of the solutions work for me:

public void uploadImage2(){
        String file_path = selectedImage.getLastPathSegment();
        File from = new File(file_path,"old");
        File to = new File("pic");
        from.renameTo(to);
        //create reference to images folder and assing a name to the file that will be uploaded
        imageRef = storageRef.child(getNickname() + "/" + from.getAbsolutePath());

To solve this, every time you upload a new picture to Firebase Storage, generate a unique id for each one beneath the user folder. To achieve this, you can simply use the push() method. From the offical documentation :

Create a reference to an auto-generated child location.

我知道了,只是写

imageRef = storageRef.child(getNickname() + "/pic");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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