在android項目中訪問網絡圖片是非常普遍性的事情,如果我們每次請求都要訪問網絡來獲取圖片,會非常耗費流量,而且圖片占用內存空間也比較大,圖片過多且不釋放的話很容易造成內存溢出。針對上面遇到的兩個問題,首先耗費流量我們可以將圖片第一次加載上面緩存到本地,以后如果本地有就直接從本地加載。圖片過多造成內存溢出,這個是最不容易解決的,要想一些好的緩存策略,比如大圖片使用lru緩存策略或懶加載緩存策略,首先介紹一下本地緩存圖片。
首先看一下異步加載緩存本地代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
public class asyncbitmaploader { /** * 內存圖片軟引用緩沖 */ private hashmap<string, softreference<bitmap>> imagecache = null ; public asyncbitmaploader() { imagecache = new hashmap<string, softreference<bitmap>>(); } public bitmap loadbitmap( final imageview imageview, final string imageurl, final imagecallback imagecallback) { //在內存緩存中,則返回bitmap對象 if (imagecache.containskey(imageurl)) { softreference<bitmap> reference = imagecache.get(imageurl); bitmap bitmap = reference.get(); if (bitmap != null ) { return bitmap; } } else { /** * 加上一個對本地緩存的查找 */ string bitmapname = imageurl.substring(imageurl.lastindexof( "/" ) + 1 ); file cachedir = new file( "/mnt/sdcard/test/" ); file[] cachefiles = cachedir.listfiles(); int i = 0 ; if ( null !=cachefiles){ for (; i<cachefiles.length; i++) { if (bitmapname.equals(cachefiles[i].getname())) { break ; } } if (i < cachefiles.length) { return bitmapfactory.decodefile( "/mnt/sdcard/test/" + bitmapname); } } } final handler handler = new handler() { /* (non-javadoc) * @see android.os.handler#handlemessage(android.os.message) */ @override public void handlemessage(message msg) { // todo auto-generated method stub imagecallback.imageload(imageview, (bitmap)msg.obj); } }; //如果不在內存緩存中,也不在本地(被jvm回收掉),則開啟線程下載圖片 new thread() { /* (non-javadoc) * @see java.lang.thread#run() */ @override public void run() { // todo auto-generated method stub inputstream bitmapis = httputils.getstreamfromurl(imageurl); bitmap bitmap = bitmapfactory.decodestream(bitmapis); imagecache.put(imageurl, new softreference<bitmap>(bitmap)); message msg = handler.obtainmessage( 0 , bitmap); handler.sendmessage(msg); file dir = new file( "/mnt/sdcard/test/" ); if (!dir.exists()) { dir.mkdirs(); } file bitmapfile = new file( "/mnt/sdcard/test/" + imageurl.substring(imageurl.lastindexof( "/" ) + 1 )); if (!bitmapfile.exists()) { try { bitmapfile.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } fileoutputstream fos; try { fos = new fileoutputstream(bitmapfile); bitmap.compress(bitmap.compressformat.png, 100 , fos); fos.close(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }.start(); return null ; } public interface imagecallback { public void imageload(imageview imageview, bitmap bitmap); } } |
這是一個封裝好的異步加載圖片類,緩存了兩份,一份是使用軟引用緩存到內存中,一份是緩存到本地sd卡,如果內存中沒有,則從本地查找,如果本地沒有則從網絡獲取圖片。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class httputils { public static inputstream getstreamfromurl(string imageurl) { inputstream in= null ; try { url url= new url(imageurl); httpurlconnection connection=(httpurlconnection) url.openconnection(); in=connection.getinputstream(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return in; } } |
這是一個訪問網絡獲取類,不細說了。
下面看一下如何使用封裝好的異步加載圖片的類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
public class imagecacheactivity extends activity { /** called when the activity is first created. */ private listview listview; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); listview=(listview) findviewbyid(r.id.listview_list); myadapter adapter= new myadapter(); listview.setadapter(adapter); } private class myadapter extends baseadapter{ private asyncbitmaploader asyncbitmaploader; public myadapter(){ asyncbitmaploader= new asyncbitmaploader(); } @override public int getcount() { // todo auto-generated method stub return 10 ; } @override public object getitem( int position) { // todo auto-generated method stub return null ; } @override public long getitemid( int position) { // todo auto-generated method stub return 0 ; } @override public view getview( int position, view convertview, viewgroup parent) { // todo auto-generated method stub if (convertview== null ){ convertview=layoutinflater.from(getapplicationcontext()).inflate(r.layout.list_item, null ); } imageview image=(imageview) convertview.findviewbyid(r.id.addexam_list_icon); string imageurl= "http://s.ata.net.cn/4f98db46908987a21a000003/logo/2012/04/114_80aaf295c083d07a496743699aac3193.png" ; bitmap bitmap=asyncbitmaploader.loadbitmap(image, imageurl, new imagecallback() { @override public void imageload(imageview imageview, bitmap bitmap) { // todo auto-generated method stub imageview.setimagebitmap(bitmap); } }); if (bitmap == null ) { image.setimageresource(r.drawable.ic_launcher); } else { image.setimagebitmap(bitmap); } return convertview; } } } |
這樣就完成了,網絡獲取不到bitmap則顯示默認圖片。
這是一種很實用的方法,大家自己試試吧!
希望本文所述對大家學習android軟件編程有所幫助。