博客
关于我
安卓Kotlin 用GET方法保存文件到filesDir
阅读量:419 次
发布时间:2019-03-05

本文共 972 字,大约阅读时间需要 3 分钟。

HttpURLConnection需要通过新建Thread以执行网络请求

        fun downloadUsingUrl(url: String?) {            Thread(Runnable {                try {                    val connection = URL(url).openConnection() as HttpURLConnection                    connection.requestMethod = "GET"                    connection.connectTimeout = 8000                    connection.readTimeout = 8000                    val avatarFile = File("$filesDir/avatar.png")                    if (avatarFile.exists()) {                        avatarFile.delete()                    } else if (!filesDir.exists()) {                        filesDir.mkdir()                    }                    filesDir.setReadable(true)                    filesDir.setWritable(true)                    connection.inputStream.buffered().copyTo(avatarFile.outputStream())                } catch (ex: Exception) {                    ex.printStackTrace()                }            }).start()        }    

转载地址:http://grfzz.baihongyu.com/

你可能感兴趣的文章
openGL学习步骤
查看>>
OpenGL的基本概念介绍
查看>>
OpenGL着色器、纹理开发案例
查看>>
OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll(转))
查看>>
opengl绘制几何体的函数
查看>>
openGL缓存概念和缓存清除(01)
查看>>
OpenJDK11 下的HSDB工具使用入门
查看>>
openjdk踩坑
查看>>
openjudge 1792 迷宫 解析报告
查看>>
OpenJudge/Poj 1658 Eva's Problem
查看>>
Openlayers 9.0新功能
查看>>
openlayers controls基础知识
查看>>
Openlayers Draw的用法、属性、方法、事件介绍
查看>>
Openlayers Interaction基础及重点内容讲解
查看>>
Openlayers layer 基础及重点内容讲解
查看>>
Openlayers map三要素(view,target,layers),及其他参数属性方法介绍
查看>>
Openlayers Map事件基础及重点内容讲解
查看>>
Openlayers Overlay基础及重点内容讲解
查看>>
Openlayers Select的用法、属性、方法、事件介绍
查看>>
Openlayers Source基础及重点内容讲解
查看>>