Android - HttpURLConnection下载网络文件,加载网络图片
来源:互联网 发布:剑网3成男捏脸数据 编辑:程序博客网 时间:2024/06/05 12:05
说明:
做sdk开发的时候(sdk不采取任何第三方框架),涉及到下载网络文件,和加载网络图片的功能,由于不能用第三方jar包进行,所以只能用基本的HttpURLConnection把文件作为流来处理,进行下载和加载。
1、HttpURLConnection加载图片
代码:
/** * 加载图片ImageView * @param url 图片网络地址 * @return */ public void setImageViewBitMap(ImageView imageView, final String url) { this.imageView = imageView; new Thread() { @Override public void run() { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } imageBitmap = bitmap; myHandler.sendEmptyMessage(1); } }.start(); }
2、HttpURLConnection下载文件
代码:
/** * 把js,css保存在本地 * @param uFile 本地存储的文件名File路径 * @param url 将要下载的js,css文件 */ public static void writeUrToStrealm(File uFile, String url) { try { URL uri = new URL(url); URLConnection connection = uri.openConnection(); InputStream uristream = connection.getInputStream(); //String cache = connection.getHeaderField("Ddbuild-Cache"); String contentType = connection.getContentType(); //textml; charset=utf-8 String mimeType = ""; String encoding = ""; if (contentType != null && !"".equals(contentType)) { if (contentType.indexOf(";") != -1) { String[] args = contentType.split(";"); mimeType = args[0]; String[] args2 = args[1].trim().split("="); if (args.length == 2 && args2[0].trim().toLowerCase().equals("charset")) { encoding = args2[1].trim(); } else { encoding = "utf-8"; } } else { mimeType = contentType; encoding = "utf-8"; } } //todo:缓存uristream FileOutputStream output = new FileOutputStream(uFile); int read_len; byte[] buffer = new byte[1024]; SaveDataUtils.writeBlock(output, mimeType); SaveDataUtils.writeBlock(output, encoding); while ((read_len = uristream.read(buffer)) > 0) { output.write(buffer, 0, read_len); } output.close(); uristream.close(); } catch (Exception e) { e.printStackTrace(); } }
0 0
- Android - HttpURLConnection下载网络文件,加载网络图片
- Android HttpURLConnection 加载网络图片
- HttpURLConnection下载网络图片
- HttpURLConnection下载网络图片
- android httpUrlConnection 方式从网络上面下载图片
- Android HttpURLConnection获取网络下载图片POST请求方式
- android网络HttpURLConnection抓取网络图片
- android加载网络图片,并下载
- 使用HttpURLConnection从网络中下载图片
- AsyncTask与HttpURLConnection下载网络图片
- 下载网络文件HttpURLConnection.getContentLength()大小为
- 使用HttpURLConnection访问网络——加载网络图片
- android service HttpURLConnection下载网络数据
- android加载网络图片
- Android加载网络图片
- android加载网络图片
- android 网络加载图片
- android加载网络图片
- <context:annotation-config> 、<mvc:annotation-driven>的区别
- DVR_RDK_McFW_Link_API_Training.pdf 中文
- ubuntu内的微信客户端【非官方】
- 38款 流媒体服务器开源软件
- sts(spring toll suite)默认编码设置
- Android - HttpURLConnection下载网络文件,加载网络图片
- Android 5.0特新中ViewAnimationUtils的使用
- Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍
- bad block 坏块修复
- JNI 之二 :java & c/c++ 相互通信及调用
- mybatis逆向生成(eclipse)
- android和JavaScript交互
- 打造一个炫酷的EventBus
- Spring Cloud构建微服务架构(四)分布式配置中心(续)