Search code examples
javaandroidfirebasefirebase-storage

How to add OnProgressListener for Firebase Storage File Downloadings?


I am trying to create a method for downloading my 3D files stored in the Firebase Storage with OnProgressListener and show the downloaded percentage for the user with a progressBar, but it is not working as expected progress_percentage, i don't know why my code inside the OnProgressListener is not working.

I followed these docs to tryout what i intended to do.

https://firebase.google.com/docs/storage/android/download-files#java

https://github.com/firebase/snippets-android/blob/bdab1d019d41c1e034ecc1d63d30c7071af3d79d/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L346-L357

this is how my method looks like.I feel i have done something wrong in in my code, I am expecting some help to correct my code and make this working..

    private void download3dFile(String fileUrl){
        downloadProgressLayout.setVisibility(View.VISIBLE);

        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference httpReference = storage.getReferenceFromUrl(fileUrl);
        File localFile;

        try {
            localFile = File.createTempFile("model","glb");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        httpReference.getFile(localFile).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onProgress(@NonNull FileDownloadTask.TaskSnapshot taskSnapshot) {
                long percentage = taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount()*100;
                progressPercentageTxt.setText(MessageFormat.format("{0}%", percentage));
                progressBar.setProgress((int) percentage);
            }
        }).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> taskSnapshot) {
                Toast.makeText(MainActivity.this, "Download Complete", Toast.LENGTH_SHORT).show();
                downloadProgressLayout.setVisibility(View.GONE);
                Uri fileUri = Uri.fromFile(localFile);
                Toast.makeText(MainActivity.this, "Uri - "+fileUri, Toast.LENGTH_LONG).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

            }
        }).addOnCanceledListener(new OnCanceledListener() {
            @Override
            public void onCanceled() {
                Toast.makeText(MainActivity.this, "Download Canceled!", Toast.LENGTH_SHORT).show();
                downloadProgressLayout.setVisibility(View.GONE);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(MainActivity.this, "Download Failed - "+e.getMessage(), Toast.LENGTH_SHORT).show();
                downloadProgressLayout.setVisibility(View.GONE);
            }
        });


    }

Solution

  • It seems your code is just working fine, if you are hitting inside of onProgress callback, the code should work, change the code inside onProgress Callback as follows.

    double percentage = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
    progressPercentageTxt.setText(MessageFormat.format("{0}%", (int) percentage));
    progressBar.setProgress((int) percentage);