Snippets Collections
  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")
                }
            })
    }
    private fun downloadPDF(
        filUrl: String,
        filePath: String,
        completion: (response: String) -> Unit
    ) {

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

        val executor2: ExecutorService = Executors.newSingleThreadExecutor()
        val handler2 = Handler(Looper.getMainLooper())
        executor2.execute {
            //Background work here
            var inputStream: InputStream? = null
            handler2.post {
                val url = URL(filUrl)
                val urlConnection: HttpURLConnection =
                    url.openConnection() as HttpsURLConnection
                if (urlConnection.responseCode == 200) {
                    inputStream = BufferedInputStream(urlConnection.inputStream)
                    try {
                        inputStream = url.openStream()
                        val outputStream = FileOutputStream(filePath)
                        val buffer = ByteArray(1024)

                        //var bytesRead = inputStream.read(buffer)
                        val fileLength: Int = urlConnection.getContentLength()
                        var total: Long = 0
                        var count: Int

                        while (inputStream!!.read(buffer).also { count = it } != -1) {
                            total += count
                            // publishing the progress....
                            if (fileLength > 0) {
                                Log.e(
                                    TAG,
                                    "download_progress: " + (total * 100 / fileLength).toInt()
                                )
                                runOnUiThread {
                                    if (!(total * 100 / fileLength).toInt().toString()
                                            .equals(binding.progressTextView.text)
                                    ) {
                                        binding.progressTextView.setText(
                                            (total * 100 / fileLength).toInt().toString()
                                        )
                                        binding.progressBar.setProgress((total * 100 / fileLength).toInt())
                                    }
                                }
                            } // only if total length is known
                            else {
                                Log.e(
                                    TAG,
                                    "download_progress: " + fileLength
                                )
                            }


                            outputStream.write(buffer, 0, count)
                        }

                        outputStream.close()
                        inputStream!!.close()
                        urlConnection.disconnect();

                        //dialogProgress.dismiss()
                        completion("1")

                    } catch (e: Exception) {
                        e.printStackTrace()
                        completion(e.printStackTrace().toString())
                    }

                }

            }
        }

    }

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension