Android retrofit 파일 다운로드


private val myRetrofitCallback = (object : retrofit2.Callback<ResponseBody>{
        override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
            println("download error : " + t.message.toString())
        }

        override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
            var inputS : InputStream = response.body()!!.byteStream()
            var bmp : Bitmap = BitmapFactory.decodeStream(inputS)
            //binding.imageView3.setImageBitmap(bmp)

        }
    })

    fun saveFile(body: ResponseBody?, pathWhereYouWantToSaveFile: String):String{
        if (body==null)
            return ""
        var input: InputStream? = null
        try {
            input = body.byteStream()
            //val file = File(getCacheDir(), "cacheFileAppeal.srl")
            val fos = FileOutputStream(pathWhereYouWantToSaveFile)
            fos.use { output ->
                val buffer = ByteArray(4 * 1024) // or other buffer size
                var read: Int
                while (input.read(buffer).also { read = it } != -1) {
                    output.write(buffer, 0, read)
                }
                output.flush()
            }
            return pathWhereYouWantToSaveFile
        }catch (e:Exception){
            Log.e("saveFile",e.toString())
        }
        finally {
            input?.close()
        }
        return ""
    }
,

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다