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 ...
随机推荐
- 关于新一代Android的一切Android L (2014-07-04)
谷歌在今年的I/O大会上一改曾经的传统,由发布新版Android改为发布Android L的开发者预览版本,而其正式版本将会在今年秋天面世,这种方式将会方便开发者在正式版发布之前尽早对自己应用进行优化 ...
- C99规范
. 增加restrict指针 C99中增加了公适用于指针的restrict类型修饰符,它是初始访问指针所指对象的惟一途径,因此只有借助restrict指针表达式才能访问对象.restrict指针指针主 ...
- Python性能优化:PyPy、Numba 与 Cython。PyPy的安装及对应pip的安装
性能优化讨论见参考1:大概意思是,PyPy内置JIT,对纯Python项目兼容性极好,几乎可以直接运行并直接获得性能提升:缺点是对很多C语言库支持性不好.Numba是一个库,可以在运行时将Python ...
- 【转载】Redis在新浪微博中的应用
转载自文章 http://blog.me115.com/2013/12/19/redis-e5-9c-a8-e6-96-b0-e6-b5-aa-e5-be-ae-e5-8d-9a-e4-b8-ad-e ...
- 使用PowerDesigner建立数据库模型【转】
1. 打开PowerDesigner,点击File->New 2. 选择Conceptual Data Model,并修改Model name. 3. 在Palette工具栏中点击Entity ...
- UDP套接字——(DGRAM)
/*********************程序相关信息********************* * 程序编号:014 * 程序编写起始日期:2013.11.29 * 程序编写完成日期:2013.1 ...
- perforce 使用教程(zz)
http://www.perforce.com/documentation/perforce_technical_documentation http://blog.csdn.net/brucexu1 ...
- Android -- 写xml到SD卡中
信息类 private ...
- php 上传视频的代码,
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- (算法)Hanoi Problem汉诺塔问题
Problem: There are three poles and N disks where each disk is heaver than the next disk. In the init ...