[英]Rename picture before uploading to Firebase Storage
The selected picture from the user is selectedImage . 从用户选择的图片为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. 例如,如果david上传图片,它将放在“ david”文件夹下,但是我希望每个上传的图片都具有相同的文件名。 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. 目标:如果david上传1张图片,然后再上传另一张图片,则第二张图片将替换第一张图片,因为它们都具有相同的文件名。 I tried doing renameTo but instead it uploads under a new folder under david/imagename/old.jpg 我试着做renameTo,但是它在david / imagename / old.jpg下的新文件夹下上传
I want it to be like this david/pic.jpg that's it. 我希望它像这样的david / pic.jpg 。 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. 为了解决这个问题,每次将新图片上传到Firebase Storage时,都会为用户文件夹下的每个图片生成唯一的ID。 To achieve this, you can simply use the push() method. 为此,您可以简单地使用push()方法。 From the offical documentation : 从官方文档中 :
Create a reference to an auto-generated child location. 创建对自动生成的子位置的引用。
我知道了,只是写
imageRef = storageRef.child(getNickname() + "/pic");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.