3
votes

I have image view which loads image using Glide using download link. Code is given below :

How can I get the file name stored in fire base storage from the download url ?

Glide.with(ct)
  .load(downloadurllink)
  .centerCrop();

placeholder(R.drawable.common_icon_dark)
  .into(holder.image);
6
You need to save uploaded file/image url to store realtime database and get from realtime databaseAnas Mehar
@AnasMehar You have a point!Gaurav Mall
@snth Did something help you or not?Gaurav Mall
@ Gaur Mall I stored and retrived from database and thanks for your code too.snth

6 Answers

4
votes

I had the same problem and I used a Regular Expression to extract the file name from the download URL

%2..*%2F(.*?)\?alt

Eg: If your download URL is https://firebasestorage.googleapis.com/v0/b/art-track.appspot.com/o/images%2Fu1nffdGQ7QPLIMp7N11vSOYorUM2%2FCapture.JPG?alt=media&token=86081f67-9065-4a13-aa0b-14fab7d44bf3 , by using %2..*%2F(.*?)\?alt you can extract "Capture.JPG"

3
votes

You can get filename easily using the name property. Example:

val httpsReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/art-                     
track.appspot.com/o/images%2Fu1nffhbfdjsa%2FN11vSOYorUM2%2FImageName.JPG? 
alt=media&token=86081f67-9065-4a13-aa0b-14fab7d44bf3")

Log.d(TAG, "filename: ${httpsReference.name}")
3
votes

If the URL is in this pattern:

https://firebasestorage.googleapis.com/v0/b/art-track.appspot.com/o/images%2Fu1nffhbfdjsa%2FN11vSOYorUM2%2FImageName.JPG?alt=media&token=86081f67-9065-4a13-aa0b-14fab7d44bf3

then this will work to split the URL into the pattern from 2F till 2F and it will remove the extension (.jpg or .png)

String url="https://firebasestorage.googleapis.com/v0/b/art-                     
track.appspot.com/o/images%2Fu1nffhbfdjsa%2FN11vSOYorUM2%2FImageName.JPG? 
alt=media&token=86081f67-9065-4a13-aa0b-14fab7d44bf3";

print(url.split(RegExp(r'(%2F)..*(%2F)'))[1].split(".")[0]);
2
votes

There are many options. Two of the most used ones are:

1. You can use File Metadata in Firebase Storage to get the file name from URL. Basically, you create a Firebase Data Reference and then add a file metadata listener, like this:

    // Create a storage reference from our app
    StorageReference storageRef = storage.getReference();

    // Get reference to the file
    StorageReference fileRef = storageRef.child("images/forest.jpg");

    fileRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
        @Override
        public void onSuccess(StorageMetadata storageMetadata) {
            String filename = storageMetadata.getName();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
        }
    });

This code was taken from the documentation which I advice you to check out: File Metadata

2. Another option is to store your URL with the name in Firebase Database. This has the advantage of avoiding unnecessary listeners. That means that you can get the name with a single value event listener without having to load all of the file metadata.

    database.child("files").child("url")
    .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              String name = dataSnapShot.getValue(String.class);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    }

It depends on your implementation on how you want to do it. Hope it helps :)

1
votes

The easiest way is to use the reference method:

let storage = Storage.storage()
let yourFirestoreURL = "https://firebasestorage.googleapis.com/v0/b/art-track.appspot.com/o/images%2Fu1nffdGQ7QPLIMp7N11vSOYorUM2%2FCapture.JPG?alt=media&token=86081f67-9065-4a13-aa0b-14fab7d44bf3"
let storageRef = storage.reference(forURL: yourFirestoreURL)

print(storageRef.name)

For further details, see the google guide here: https://firebase.google.com/docs/storage/ios/create-reference

-1
votes

Solution for iOS!

    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"%2F(.*?)\\?alt" options:NSRegularExpressionCaseInsensitive error:&error];
    NSArray *matches = [regex matchesInString:photo_url options:0 range:NSMakeRange(0, [photo_url length])];
    if (matches && matches.count > 0) {
        NSString *substringForFirstMatch = [photo_url substringWithRange:[matches[0] rangeAtIndex:1]];
    }