简体   繁体   中英

Android Firebase Storage: How to get file size from firebase storage file url before downloading it?

How can I get file size from firebase storage before downloading it? I want to show file size to user so that they can see what is the exact size of the file.

I looked uphttps://firebase.google.com/docs/storage/android/download-files to check if there is any method to get particular file's size before downloading it. But I didn't find anything helpful.

Try this:

StorageReference ref = storage.getReference();
StorageReference reference = ref.child("images.jpg");

reference.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
    Log.i("The size of the file is:", storageMetadata.getSizeBytes());
  }
}).addOnFailureListener(new OnFailureListener() {
   @Override
   public void onFailure(@NonNull Exception exception) {

    }
});

Using getSizeBytes() you will be able to get the size of the file.

public long getSizeBytes ():

Returns the stored Size in bytes of the StorageReference object

public StorageMetadata ():

Creates a StorageMetadata object to hold metadata for a StorageReference.

For more info check this:

https://developers.google.com/android/reference/com/google/firebase/storage/StorageMetadata.html https://firebase.google.com/docs/storage/android/file-metadata

As posted on Firebase docs, you can get name, size, and content type of file stored on FirebaseStorage, by StorageReference getMetaData() method. According to Firebase docs,

File metadata contains common properties such as name, size, and contentType (often referred to as MIME type) in addition to some less common ones like contentDisposition and timeCreated. This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method.

Here's the example how to access metadata of file stored on FirebaseStorage,

StorageReference storageRef = storage.getReference();
StorageReference forestRef = storageRef.child("images/forest.jpg");

forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
    @Override
    public void onSuccess(StorageMetadata storageMetadata) {
        //here, storageMetadata contains all details about your file stored on FirebaseStorage
        Log.i("tag", "name of file: "+storageMetadata.getName());
        Log.i("tag", "size of file in bytes: "+storageMetadata.getSizeBytes());
        Log.i("tag", "content type of file: "+storageMetadata.getContentType());
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
        Log.i("tag", "Exception occur while gettig metadata: "+exception.toString());
    }
});

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