abstract class DownloadObserver(var url: String) : DisposableObserver<ResponseBody>() {
override fun onComplete() {}
override fun onNext(responseBody: ResponseBody) {
val fileName = url.substring(url.lastIndexOf("/"), url.length)
val dirPath = Environment.getExternalStorageDirectory().absolutePath + "/" + MyApp.mContext.packageName
val dirFile = File(dirPath)
if (!dirFile.exists() && !dirFile.isDirectory) {
dirFile.mkdirs()
}
val file = File("$dirPath/$fileName")
val inputStream = responseBody.byteStream()
val buffer = ByteArray(1024 * 4)
var fos: FileOutputStream? = null
var sum = 0
var len = 0
val off = 0
try {
fos = FileOutputStream(file)
while (inputStream.read(buffer).apply { len = this } > 0)
Android Retrofit下载文件
最新推荐文章于 2024-05-06 15:05:37 发布