×

Android异步从网络下载图片并且缓存图片到本地(三)

Kalet Kalet 发表于2014-01-04 19:38:54 浏览275 评论0

抢沙发发表评论

095    /**
096     * 采用普通方式异步的加载图片
097     */
098    /*private void asyncloadImage(final ImageView iv_header, final String path) {
099        final Handler mHandler = new Handler() {
100            @Override
101            public void handleMessage(Message msg) {
102                super.handleMessage(msg);
103                if (msg.what == SUCCESS_GET_IMAGE) {
104                    Uri uri = (Uri) msg.obj;
105                    if (iv_header != null && uri != null) {
106                        iv_header.setImageURI(uri);
107                    }
108 
109                }
110            }
111        };
112        // 子线程,开启子线程去下载或者去缓存目录找图片,并且返回图片在缓存目录的地址
113        Runnable runnable = new Runnable() {
114            @Override
115            public void run() {
116                ContactService service = new ContactService();
117                try {
118                    //这个URI是图片下载到本地后的缓存目录中的URI
119                    Uri uri = service.getImageURI(path, cache);
120                    Message msg = new Message();
121                    msg.what = SUCCESS_GET_IMAGE;
122                    msg.obj = uri;
123                    mHandler.sendMessage(msg);
124                } catch (Exception e) {
125                    e.printStackTrace();
126                }
127            }
128        };
129        new Thread(runnable).start();
130    }*/
131}

 

       自定义Adapter中,我们要注意 AsyncImageTask这个类继承了AsyncTask类,AsyncTask是Android中常用来做异步任务的类,对线程池进行了封装,详细分析稍后再贴出一篇Blog。
  下面是我们从服务器上获取并且解析的Xml文件
01<?xml version="1.0" encoding="UTF-8"?>
02<contacts>
03    <contact id="1">
04        <name>张飞</name>
05        <image src="http://192.168.1.103:8080/mymyweb/images/1.gif"/>
06    </contact>
07    <contact id="2">
08        <name>博文</name>
09        <image src="http://192.168.1.103:8080/myweb/images/2.gif"/>
10    </contact>   
11    <contact id="3">
12        <name>张天佑</name>
13        <image src="http://192.168.1.103:8080/myweb/images/3.gif"/>
14    </contact>       
15    <contact id="4">
16        <name>松德</name>
17        <image src="http://192.168.1.103:8080/myweb/images/4.gif"/>
18    </contact>       
19    <contact id="5">
20        <name>赵薇</name>
21        <image src="http://192.168.1.103:8080/myweb/images/5.gif"/>
22    </contact>
23    <contact id="6">
24        <name>李静</name>
25        <image src="http://192.168.1.103:8080/myweb/images/6.gif"/>
26    </contact>   
27    <contact id="7">
28        <name>李明</name>
29        <image src="http://192.168.1.103:8080/myweb/images/7.gif"/>
30    </contact>       
31    <contact id="8">
32        <name>黎明</name>
33        <image src="http://192.168.1.103:8080/myweb/images/8.gif"/>
34    </contact>       
35     
36    <contact id="9">
37        <name>秦桧</name>
38        <image src="http://192.168.1.103:8080/myweb/images/9.gif"/>
39    </contact>
40    <contact id="10">
41        <name>朱德</name>
42        <image src="http://192.168.1.103:8080/myweb/images/10.gif"/>
43    </contact>   
44    <contact id="11">
45        <name>冯巩</name>
46        <image src="http://192.168.1.103:8080/myweb/images/11.gif"/>
47    </contact>       
48    <contact id="12">
49        <name>dylan</name>
50        <image src="http://192.168.1.103:8080/myweb/images/12.gif"/>
51    </contact>       
52    <contact id="13">
53        <name>黄单</name>
54        <image src="http://192.168.1.103:8080/myweb/images/13.gif"/>
55    </contact>
56    <contact id="14">
57        <name>含蕊</name>
58        <image src="http://192.168.1.103:8080/myweb/images/14.gif"/>
59    </contact>   
60    <contact id="15">
61        <name>欣琪</name>
62        <image src="http://192.168.1.103:8080/myweb/images/15.jpg"/>
63    </contact>       
64    <contact id="16">
65        <name>李忠华</name>
66        <image src="http://192.168.1.103:8080/myweb/images/16.jpg"/>
67    </contact>   
68    <contact id="17">
69        <name>方产员</name>
70        <image src="http://192.168.1.103:8080/myweb/images/17.jpg"/>
71    </contact>       
72    <contact id="18">
73        <name>张光</name>
74        <image src="http://192.168.1.103:8080/myweb/images/18.jpg"/>
75    </contact>   
76</contacts>

 

本demo中为了安全起见,还对下载下来的图片的文件名进行了MD5加密,下面是MD5加密的代码,
01public class MD5 {
02 
03    public static String getMD5(String content) {
04        try {
05            MessageDigest digest = MessageDigest.getInstance("MD5");
06            digest.update(content.getBytes());
07            return getHashString(digest);
08             
09        catch (NoSuchAlgorithmException e) {
10            e.printStackTrace();
11        }
12        return null;
13    }
14     
15    private static String getHashString(MessageDigest digest) {
16        StringBuilder builder = new StringBuilder();
17        for (byte b : digest.digest()) {
18            builder.append(Integer.toHexString((b >> 4) & 0xf));
19            builder.append(Integer.toHexString(b & 0xf));
20        }
21        return builder.toString();
22    }
23}

 

      以上省略了Contact.java这个domain类,通过这个demo,可以看出Android中会经常需要进行异步任务的处理,所以我们会常常用到自己手动开启线程,handler机制,或者AsyncTask类等手段来保证应用的性能。

群贤毕至

访客