I'm trying to upload images to Firebase Storage and add the download URL using getDownloadUrl()
method to Firebase Database .
I want to check if the URL already exists in Firebase Database or not before adding it, I tried to split the URL using StringUtils.substringBetween(mUrl, "%2F", ".png")
; for example :
https://firebasestorage.googleapis.com/v0/b/app.appspot.com/o/Frames%2FAPP1565024974054.png
After Splitting I get this which is the name of the file :
APP1565024974054.png
So I can check if this string exists in Database or not before adding it.
Here's my code :
gettingURLs method: to get all URLs from Database and adding them to 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();
}
});
}
verification method : to check if URL exists or not
private void verification(ArrayList<String> arrayList, String mUrl) {
if (arrayList.size() > 0) {
String PREFIX = StringUtils.substringBetween(mUrl, "%2F", ".png");
for (String str : arrayList) {
if (str.trim().contains(PREFIX)) {
Toast.makeText(createFrame.this, "Frame already uploaded, You don't need to upload it again", Toast.LENGTH_SHORT).show();
Helper.dismissProgressDialog();
break;
} else {
addUrlToDataBase(mUrl, mDatabase);
break;
}
}
} else
addUrlToDataBase(mUrl, mDatabase);
}
My issue :
code above adds all URLs although it already exists in the database, how I can check if URL exist before adding it
1 - If the URL exists, show Toast with
Toast.makeText(createFrame.this, "Frame already uploaded, You don't need to upload it again", Toast.LENGTH_SHORT).show();
2 - If the URL does not exist, Add it normally
Every time you upload an image to Firebase storage, you have the option of generating a downloadUrl. This download url is unique, this means that if you re-upload the same image again, you'll get a different url. Therefore, comparing downloadUrls is a bad idea as they will never match.
However, there is a way you can manipulate the file name to check whether the file already exists on Firebase storage. Simply create a reference with the file name in it and try to get a download url (before pushing). The response would be null if the file does not exist. Here's a little illustration:
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
}
}
});
}
To learn more about the getDownloadUrl()
function, you can check out this post or readthe documentation .
I hope this helps. Merry coding!
You should validate the file name before uploading it to FireStorage.
Your Realtime Database structure should be like this:
photos: {
uid:{
fileName:true
}
}
So before sending the file to storage
private void shouldStore(String fileName) {
rootRef.child("photos").child(uid).child(fileName).addValueEventListenerForSingleEvent(
// ... if callback returns something dont upload
)
}
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.