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()
}
}
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) + "%...");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With