download file from url using input and output streams

PHOTO EMBED

Fri Feb 17 2023 04:33:23 GMT+0000 (Coordinated Universal Time)

Saved by @kamrantariq_123 #kotlin #downloadfile #inputstream #outputstream #stream

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

                }

            }
        }

    }
content_copyCOPY