如何在Android上显示从Firebase Storage的下载进度

如何在Android上显示从Firebase Storage的下载进度,第1张

概述如何显示从Firebase存储的下载进度.我可以下载文件,但无法显示下载进度.我怎样才能做到这一点?这是我的简单代码,可从Firebase存储区下载.mp3文件->fungetSound(dSoundIndex:Int){Toast.makeText(context.applicationContext,"StartingDownloading",Toast.LENG

如何显示从Firebase存储的下载进度.我可以下载文件,但无法显示下载进度.我怎样才能做到这一点?

这是我的简单代码,可从Firebase存储区下载.mp3文件->

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

解决方法:

使用此工具,您可以从Firebase存储下载文件,进度监听器将为您提供进度.希望能帮助到你

//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) + "%...");        }    });
总结

以上是内存溢出为你收集整理的如何在Android上显示从Firebase Storage的下载进度全部内容,希望文章能够帮你解决如何在Android上显示从Firebase Storage的下载进度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1080812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存