android从本地缓存和网络加载图片
2016-03-17 15:50
531 查看
一、实现的思路
1、获取网络地址path
2、建立url对象
3、建立url连接conn
4、设置conn的连接方法
5、获取conn的输入流
7、new一个byte[] 数组
8、建立一个本地的文件File,并获取file的outputstream
9、用outputstream将byte[]写入到文件中
10、bitmapfactory.decodefile(),获取bitmap对象,并显示到图片控件上。
二、代码
public void loadImg(){
String path="http://192.168.2.101:8080/ppa_web/image/wsn.jpg";
File file =new File(getCacheDir(), getFileName(path));
if (file.exists()) {
System.out.println("从本地缓存加载图片");
Bitmap bp=BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg=new Message();
msg.obj=bp;
Handler.sendMessage(msg);
}else {
System.out.println("从网络加载图片");
try {
URL url = new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.connect();
if (conn.getResponseCode()==200) {
InputStream is=conn.getInputStream();
FileOutputStream fos=new FileOutputStream(file);
byte[] b=new byte[1024];
int len;
while ((len=is.read(b))!=-1) {
fos.write(b, 0, len);
}
Bitmap bp=BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg=new Message();
msg.obj=bp;
Handler.sendMessage(msg);
}else {
Toast.makeText(this, "访问网络失败", 2000).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//根据网络地址获取文件名称
private String getFileName(String path) {
int index=path.lastIndexOf("/");
return path.substring(index);
}
1、获取网络地址path
2、建立url对象
3、建立url连接conn
4、设置conn的连接方法
5、获取conn的输入流
7、new一个byte[] 数组
8、建立一个本地的文件File,并获取file的outputstream
9、用outputstream将byte[]写入到文件中
10、bitmapfactory.decodefile(),获取bitmap对象,并显示到图片控件上。
二、代码
public void loadImg(){
String path="http://192.168.2.101:8080/ppa_web/image/wsn.jpg";
File file =new File(getCacheDir(), getFileName(path));
if (file.exists()) {
System.out.println("从本地缓存加载图片");
Bitmap bp=BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg=new Message();
msg.obj=bp;
Handler.sendMessage(msg);
}else {
System.out.println("从网络加载图片");
try {
URL url = new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.connect();
if (conn.getResponseCode()==200) {
InputStream is=conn.getInputStream();
FileOutputStream fos=new FileOutputStream(file);
byte[] b=new byte[1024];
int len;
while ((len=is.read(b))!=-1) {
fos.write(b, 0, len);
}
Bitmap bp=BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg=new Message();
msg.obj=bp;
Handler.sendMessage(msg);
}else {
Toast.makeText(this, "访问网络失败", 2000).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//根据网络地址获取文件名称
private String getFileName(String path) {
int index=path.lastIndexOf("/");
return path.substring(index);
}
相关文章推荐
- TCP/IP ---互联网的地址
- Android--网络请求
- 一个http请求的详细过程
- 使用tcp+select实现客户端与客户端的通信
- 网络编程基础+UDP的实现
- 网络加载框架Retrofit使用介绍和实例展示
- 基于NS2的差分服务网络测试(含awk分析代码)
- http://www.cocoachina.com/ios/20150901/13173.html
- kubernetes如何使用https的webapi
- TCP/IP协议、报文格式、三次握手协议
- 解决HTTP Status 500 - /pages/admin/menu/menuMgr.jsp (line: 1, column: 2) 错误
- Tomcat https 单项认证
- Java 基础高级2 网络编程
- HTTP请求WebTool
- 淘宝课程学习如何安排太原网络营销师强烈推荐
- Linux系统下几点网络基础配置方法说明
- Java程序使用httpClient发送Url链接,并获取返回的数据信息
- 使用Fiddler调试手机页面请求信息查看!
- Android 网络通信框架Volley简介(Google IO 2013)
- 从零开始写Http框架---外篇---第三方网络库