Android 网络下载图片
2中方法:
1.
public byte[] downloadResource(Context context, String url)
throws ClientProtocolException, IOException {
isStop = false;
ByteArrayBuffer buffer = null;
HttpGet hp = new HttpGet(url);
httpClient = new DefaultHttpClient();
String netType = isNetType(context);
if (netType != null & netType.equals("cmwap")) {
HttpHost proxy = new HttpHost("10.0.0.172", 80);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
proxy);
}
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
5 * 1000);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60 * 1000); HttpResponse response = httpClient.execute(hp);
if (response.getStatusLine().getStatusCode() == 200) {
inputstream = response.getEntity().getContent();
if (inputstream != null) {
int i = (int) response.getEntity().getContentLength();
buffer = new ByteArrayBuffer(1024);
byte[] tmp = new byte[1024];
int len;
while (((len = inputstream.read(tmp)) != -1)
&& (false == isStop)) {
buffer.append(tmp, 0, len);
}
}
cancel();
}
return buffer.toByteArray();
}
调用方法:
protected Bitmap doInBackground(WonderfulprogramInfo... params)
{
Bitmap bitmap = null;
try
{
String urls = Constant.url + params[0].getWonderfulImgUrl();
boolean isExists = Files.compare(urls);
if (isExists == false)
{ //网络下载图片数据
Net net = new Net();
byte[] data = net.downloadResource(HomeActivity.this, urls);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imagesCache.put(urls, bitmap); // 把下载好的图片保存到缓存中
Files.saveImage(urls, data);
} else
{ //本地读取图片数据
byte[] data = Files.readImage(urls);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// //获取资源图片
// InputStream is =
// context.getResources().openRawResource(resId);
// return BitmapFactory.decodeStream(is,null,opt); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
imagesCache.put(urls, bitmap); // 把下载好的图片保存到缓存中
}
方法二:
class DownLoadTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Integer positions;
public DownLoadTask(ImageView view, int position) {
imageView = view;
this.positions = position;
}
protected Bitmap doInBackground(String... params) {
URL url;
try {
url = new URL(params[0]);
InputStream is = url.openStream();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
HomeActivity.TopGalleryBitmap.put(positions, bitmap);
return bitmap;
} catch (OutOfMemoryError err) {
err.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
}
Android 网络下载图片的更多相关文章
- Android网络下载图片
package net.learn2develop.Networking; import android.app.Activity; import android.os.Bundle; import ...
- Android异步下载图片并且缓存图片到本地
Android异步下载图片并且缓存图片到本地 在Android开发中我们经常有这样的需求,从服务器上下载xml或者JSON类型的数据,其中包括一些图片资源,本demo模拟了这个需求,从网络上加载XML ...
- android网络编程--从网络下载图片,并保存到内存卡
功能1:从网络上取得的图片显示到imageview上面,生成Bitmap时有两种方法,一种是先转换为byte[],再生成bitmap:一种是直接用InputStream生成bitmap.功能2:点击按 ...
- android开发--下载图片
1.背景介绍 网络上图片的请求,是我们最常见的网络请求之一,不亚于对json/xml数据的请求.一般要展示给用户看的,都不会是纯粹的文字,往往都是图文信息.而在移动互联网时代,图文又往往需要最新的资讯 ...
- android 多线程下载图片
很多时候我们需要在Android设备上下载远程服务器上的图片进行显示,今天Android123整理出两种比较好的方法来实现远程图片的下载. 方法一.直接通过Android提供的Http类访问远程服 ...
- android 73 下载图片
package com.ithiema.imageviewer; import java.io.InputStream; import java.net.HttpURLConnection; impo ...
- 小记:对Android网络下载工具的初步封装!(包括json,字符串下载(volley),和图片下载(glide))
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkIn ...
- Android下载图片/调用系统相机拍照、显示并保存到本地
package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...
- iOS-UIImageView载入网络下载的图片(异步+多线程)
最原始的载入网络下载的图片方式: //最原始载入网络图片方法,相当堵塞主线程,界面卡顿 -(void)setImageWithURL:(NSString *)imageDownloadUrl{ UII ...
随机推荐
- mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据 mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结 ...
- SpEL笔记
SpEL使用示例 <bean id="chineseA" class="com.xxx.bean.Chinese" scope="prototy ...
- [转]Hamcrest使用方法实例
assertThat方法需要使用hamcrest的匹配方法: 示例 assertThat( n, allOf( greaterThan(1), lessThan(15) ) ); assertThat ...
- 覆写Activity的finish()方法
MainActivity如下: package cn.testfinish; import android.app.Activity; import android.os.Bundle; import ...
- Linux下解决 id_rsa 权限不够
问题 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVAT ...
- Java实现图片的裁剪
需求: 有一张位置大小的图片,现在需要根据这张原图得到指定尺寸的图片,且得到的图片要符合原先图片的比例,就是在原图的基础上等比例缩放得到图片后,在进行剪裁,这样保证得到的图片是原图的一部分,而不是将原 ...
- JAVA , TOMCAT , AXIS2 环境变量配置
想要成功配置Java的环境变量,那肯定就要安装JDK,才能开始配置的. 安装JDK 向导进行相关参数设置.如图: 正在安装程序的相关功能,如图: 选择安装的路径,可以自定义,也可以默认路径.如图: 成 ...
- Tensorflow 深度学习简介(自用)
一些废话,也可能不是废话.可能对,也可能不对. 机器学习的定义:如果一个程序可以在任务T上,随着经验E的增加,效果P也可以随之增加,则称这个程序可以在经验中学习. “程序”指的是需要用到的机器学习算法 ...
- 转:Tortoise SVN 版本控制常用操作知识
转自:http://www.cnblogs.com/csharp4/archive/2010/11/26/1889317.html 今天老大跑过来问我如何把SVN服务器上的当前版本回退到某一个版本上去 ...
- 外媒关注:中国版Twitter新浪微博推出微米对抗微信
Sina, China’s answer to Twitter, enters the mobile messaging battle with its own app 中国版Twitter新浪,用自 ...