download file from firebase storage with progress

PHOTO EMBED

Fri Feb 17 2023 04:35:16 GMT+0000 (Coordinated Universal Time)

Saved by @kamrantariq_123 #kotlin #downloadfile #firebase #firebasestorage #downloadprogress

  private fun downloadfile(
        filUrl: String,
        filePath: String,
        completion: (response: String) -> Unit
    ) {

        binding.progressBar.max = 100
        binding.progressBar.setProgress(0)
        binding.progressTextView.setText("0")

        val storage = FirebaseStorage.getInstance()
        val storageRef =
            storage.getReferenceFromUrl(filUrl)

        val localFile = File(filePath)

        storageRef.getFile(localFile)
            .addOnProgressListener {
                Log.e(TAG, "downloadfile: " + it.bytesTransferred + "/" + it.totalByteCount)
                if (it.totalByteCount.toInt() != -1) {
                    val progress: Double = 100.0 * it.bytesTransferred / it.totalByteCount
                    binding.progressBar.setProgress(progress.toInt())
                    binding.progressTextView.setText(progress.toInt().toString())
                }

            }
            .addOnSuccessListener(object : OnSuccessListener<FileDownloadTask.TaskSnapshot?> {
                override fun onSuccess(taskSnapshot: FileDownloadTask.TaskSnapshot?) {
                    Log.e(
                        TAG,
                        "downloadfile:onSuccess: ;local tem file created  created $localFile"
                    )
                    completion("1")
                }
            }).addOnFailureListener(object : OnFailureListener {
                override fun onFailure(@NonNull exception: java.lang.Exception) {
                    Log.e(
                        TAG,
                        "downloadfile:onFailure: ;local tem file not created  created $exception"
                    )
                    completion("downloadfile:onFailure: ;local tem file not created  created $exception")
                }
            })
    }
content_copyCOPY