0

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);
            }
        });


    }
4
  • If you put a breakpoint on the first liine of code inside of onProgress, and run in the debugger, does it reach that? If so, do all the variables have the value you'd expect? And when you step through it, still? Commented Jul 5 at 22:33
  • @FrankvanPuffelen thanks for replying, when I run the code, all the other listeners are working just fine including OnSuccess and OnComplete except OnProgress, I can't understand why only OnProgress Listener is not working
    – Agasthi
    Commented Jul 5 at 23:05
  • "is not working" is not clear enough, which is why I gave step by step instructions in my first comment. Did you set a breakpoint in the listener? Did it hit that? Commented Jul 5 at 23:27
  • @FrankvanPuffelen Yes sir, it is hitting Inside OnProgress, I also check by adding a Toast previously, the thing is long percentage = taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount()*100; is always 0, it is not updating with the BytesTransferred()
    – Agasthi
    Commented Jul 5 at 23:48

1 Answer 1

1

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);

Not the answer you're looking for? Browse other questions tagged or ask your own question.