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);
}
}

最新文章

  1. Velocity初探小结--Velocity在spring中的配置和使用
  2. 在MySQL数据库中创建一个完整的表
  3. 【vuejs小项目——vuejs2.0版本】组件化的开发方式
  4. 很久以前写的一个 ShareRestrictedSD 类
  5. [bzoj2743][HEOI2012]采花(树状数组+离线)
  6. VS2010里, using System.Data.OracleClient; 不可用
  7. mongodb php
  8. git使用相关文章的链接
  9. October 14th 2016 Week 42nd Friday
  10. 利用python爬取海量疾病名称百度搜索词条目数的爬虫实现
  11. 快速安装 GitLab 并汉化
  12. poj 3304 Segments
  13. C#高级编程笔记之第三章:对象和类型
  14. clang如何获得程序控制流图
  15. 阿里云主机Nginx下配置NodeJS、Express和Forever
  16. R语言与机器学习学习笔记
  17. oracle登陆提示“ora-01031 insufficient privileges”
  18. promise、async和await之执行顺序
  19. 使用STM32CubeMX生成USB_HOST_HID工程
  20. day75

热门文章

  1. 一次SQL查询语句的优化
  2. BIG5, GB(GB2312, GBK, ...), Unicode编码, UTF8, WideChar, MultiByte, Char说明与区别
  3. 深度学习Deep learning
  4. SRM 212 Div II Level One: YahtzeeScore
  5. 会话追踪(session tracking)
  6. OpenMP 中的线程任务调度
  7. 关于从SVN检出项目后,项目名称还是之前修改之前或者项目名称不对问题
  8. AJAX前台传过来的中文在后台获取是乱码问题
  9. 安装Office2007时出现1706错误的解决方案
  10. SSIS无法在unicode和非unicode 字符串数据类型之间转换