final String path = "http://localhost:8999/72.jpg";
final File file = new File(getCacheDir(), "dd.jpg");
if(file.exists()){//判断缓存文件是否存在
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
iv.setImageBitmap(bm);
}else{//下载图片
Thread t = new Thread(){
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b)) != -1){
fos.write(b, 0, len);
}
fos.close();
}
}
android网络连接之增加缓存
最新推荐文章于 2021-05-26 12:01:09 发布
911

被折叠的 条评论
为什么被折叠?



