Java 添加Firebase数据库之前,请检查其上是否存在URL
我正在尝试将图像上载到Firebase存储,并使用Java 添加Firebase数据库之前,请检查其上是否存在URL,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在尝试将图像上载到Firebase存储,并使用getDownloadUrl()方法将下载URL添加到Firebase数据库 我想检查该URL是否已经存在于Firebase数据库中,在添加它之前,我尝试使用StringUtils.substringBetween(mUrl,“%2F”,“.png”)拆分该URL;例如: 拆分后,我得到这个文件名: APP1565024974054.png 所以我可以在添加它之前检查这个字符串是否存在于数据库中 这是我的密码: gettingURLs方法:从数
getDownloadUrl()
方法将下载URL添加到Firebase数据库
我想检查该URL是否已经存在于Firebase数据库中,在添加它之前,我尝试使用StringUtils.substringBetween(mUrl,“%2F”,“.png”)
拆分该URL;例如:
拆分后,我得到这个文件名:
APP1565024974054.png
所以我可以在添加它之前检查这个字符串是否存在于数据库中
这是我的密码:
gettingURLs方法:从数据库获取所有URL并将它们添加到ArrayList
private void gettingURLs(String mUrl) {
ArrayList<String> arrayList = new ArrayList<>();
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String url = ds.child("url").getValue(String.class);
arrayList.add(url);
}
verification(arrayList, mUrl);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(createFrame.this, "Ops !,Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
2-如果URL不存在,则通常在每次将图像上载到Firebase存储时添加它,您可以选择生成下载URL此下载url是唯一的,这意味着如果再次上载相同的图像,您将获得不同的url。因此,比较下载url是一个坏主意,因为它们永远不会匹配 但是,您可以通过操纵文件名来检查文件是否已存在于Firebase存储中。只需创建一个包含文件名的引用,并尝试获取下载url(在推之前)。如果文件不存在,响应将为null。这里有一个小例子:
void getReferenceAndLoadNewBackground(String photoName) {
// edit this path to fit your own existing structure
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("photos").child(photoName + ".png");
/* Now, here's where the big deal is.
* If the image exists, onSuccess would be called.
* If the image does not exist, onFailure would be called but you'll still need to get the particular error. */
storageReference.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Yaay, the image exists, no need to re-upload.
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// this does not necessarily mean the image does not exist, we still need to check the error code
int code = ((StorageException) exception).getErrorCode();
if (code == StorageException.ERROR_OBJECT_NOT_FOUND) {
// haha, the image does not actually exist, upload it
}
else {
// handle all other problems here
}
}
});
}
void getReferenceAndLoadNewBackground(字符串名称){
//编辑此路径以适合您自己的现有结构
final-StorageReference-StorageReference=FirebaseStorage.getInstance().getReference().child(“照片”).child(photoName+“.png”);
/*现在,这里是重要的地方。
*如果映像存在,将调用onSuccess。
*如果映像不存在,将调用onFailure,但仍需要获取特定错误*/
storageReference.getDownloadUrl()
.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时的公共无效(Uri){
//耶,图像存在,无需重新上传。
}
})
.addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
//这并不一定意味着图像不存在,我们仍然需要检查错误代码
int code=((StorageException)exception).getErrorCode();
if(code==StorageException.ERROR\u OBJECT\u NOT\u FOUND){
//哈哈,图片其实并不存在,上传吧
}
否则{
//在这里处理所有其他问题
}
}
});
}
要了解有关getDownloadUrl()
函数的更多信息,您可以签出或阅读
我希望这有帮助。快乐的编码 在将文件上载到FireStorage之前,应验证文件名 您的实时数据库结构应如下所示:
photos: {
uid:{
fileName:true
}
}
因此,在将文件发送到存储之前
private void shouldStore(String fileName) {
rootRef.child("photos").child(uid).child(fileName).addValueEventListenerForSingleEvent(
// ... if callback returns something dont upload
)
}
问题是什么?这个代码怎么了?@AlexMamo你能检查一下我的更新帖子吗?为什么你要使用
addUrlToDataBase(mUrl,mDatabase)代码>第二次?我认为没有必要。如果我删除它,如果它不存在,我如何使用第一个addUrlToDataBase(mUrl,mDatabase)添加URL代码>,对吗?谢谢你的回答,我知道下载URL更改了,但它保持了相同的文件名,因此,我比较文件名,而不是URL本身,非常感谢,我是Firebase存储的初学者,什么是RTD?Firebase存储规则?哦!对我来说这是个错误的假设。在我的例子中,我使用实时数据库检查照片是否在上传之前。因此,在上传到FireStorage后,您还必须将文件名保存在那里
photos: {
uid:{
fileName:true
}
}
private void shouldStore(String fileName) {
rootRef.child("photos").child(uid).child(fileName).addValueEventListenerForSingleEvent(
// ... if callback returns something dont upload
)
}