如何在Android上显示从Firebase Storage的下载进度 [英] How to show download progress from Firebase Storage on Android

查看:58
本文介绍了如何在Android上显示从Firebase Storage的下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

推荐答案

使用此功能,您可以从Firebase存储中下载文件,进度监听器将为您提供进度.希望对您有帮助

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

这篇关于如何在Android上显示从Firebase Storage的下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆