Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show download progress from Firebase Storage on Android

How to show downloading progress from firebase storage. I am able to download file but not able to show progress of downloading. How can I do this?

Here is my simple code to download .mp3 file from firebase storage ->

    fun getSound(dSoundIndex : Int) {
        Toast.makeText(context.applicationContext,"Starting Downloading",Toast.LENGTH_SHORT).show()
        val mStorage = FirebaseStorage.getInstance("gs://depressioncuresounds-9e9a8.appspot.com")
        val mStorageRef = mStorage.reference

        try {

            val downloadRef = mStorageRef.root.child(downloadPath)

            val rootPath = File(Environment.getExternalStorageDirectory(),"DepressionCureSounds")
            if(!rootPath.exists()){
                rootPath.mkdirs()
            }

            val localFile = File(rootPath,"sound2.mp3")

            downloadRef.getFile(localFile).addOnSuccessListener {
                Toast.makeText(context.applicationContext, "Sound downloaded successfully", Toast.LENGTH_LONG).show()
                when(context.soundAndImagesIndex){
                    5->{
                        context.isSoundDownloaded[0] = true
                    }
                    6->{
                        context.isSoundDownloaded[1] = true
                    }
                    7->{
                        context.isSoundDownloaded[2] = true
                    }
                    8->{
                        context.isSoundDownloaded[3] = true
                    }
                    9->{
                        context.isSoundDownloaded[4] = true
                    }
                    else->{
                        Toast.makeText(context.applicationContext,"Bug in coding",Toast.LENGTH_LONG).show()
                    }
                }
            }.addOnFailureListener { exception ->
                Toast.makeText(context.applicationContext, "Failed to download file ${exception.toString()}", Toast.LENGTH_LONG).show()
            }
         } catch (e: IOException) {
            Toast.makeText(context.applicationContext, "IOException failed to download file", Toast.LENGTH_LONG).show()
        }
     }
like image 499
Sajan Josan Avatar asked Jan 12 '18 03:01

Sajan Josan


1 Answers

Using this you can download your file from Firebase storage, the progress listener will feed you the progress. Hope it helps

//Show Progress Dialog\\
    File localFile = File.createTempFile("audioFile", "mp3");
    yourStorageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            //Dismiss Progress Dialog\\

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //Dismiss Progress Dialog\\

        }
    }).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
            //calculating progress percentage
            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
            //displaying percentage in progress dialog
            yourProgressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
        }
    });
like image 152
Rahul Chandrabhan Avatar answered Sep 30 '22 09:09

Rahul Chandrabhan