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)
版权声明:本文为try_zp_catch原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。