本文實例講述了android編程實現網絡圖片查看器和網頁源碼查看器。分享給大家供大家參考,具體如下:
網絡圖片查看器
清單文加入網絡訪問權限:
1
2
|
<!-- 訪問internet權限 --> <uses-permission android:name= "android.permission.internet" /> |
界面如下:
示例:
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
|
public class mainactivity extends activity { private edittext imagepath; private imageview imageview; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); imagepath = (edittext) this .findviewbyid(r.id.imagepath); imageview = (imageview) this .findviewbyid(r.id.imageview); button button = (button) this .findviewbyid(r.id.button); button.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { string path = imagepath.gettext().tostring(); try { byte [] data = imageservice.getimage(path); //獲取圖片數據 if (data!= null ){ //構建位圖對象 bitmap bitmap = bitmapfactory.decodebytearray(data, 0 , data.length); imageview.setimagebitmap(bitmap); //顯示圖片 } else { toast.maketext(getapplicationcontext(), r.string.error, 1 ).show(); } } catch (exception e) { toast.maketext(getapplicationcontext(), r.string.error, 1 ).show(); } } }); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class imageservice { /** * 獲取圖片 * @param path 網絡圖片路徑 * @return 圖片的字節數據 */ public static byte [] getimage(string path) throws exception{ url url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); //設置超時時間 conn.setconnecttimeout( 5000 ); conn.setrequestmethod( "get" ); if (conn.getresponsecode()== 200 ){ inputstream instream = conn.getinputstream(); byte [] data = streamtool.read(instream); return data; } return null ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class streamtool { /** * 讀取輸入流數據 * @param instream * @return */ public static byte [] read(inputstream instream) throws exception{ bytearrayoutputstream outstream = new bytearrayoutputstream(); byte [] buffer = new byte [ 1024 ]; int len = 0 ; while ( (len = instream.read(buffer)) != - 1 ){ outstream.write(buffer, 0 , len); } instream.close(); return outstream.tobytearray(); } } |
網頁源碼查看器
如果網頁的源碼超過屏幕的顯示位置的話,要求出現滾動條.
1
2
3
4
5
6
7
8
9
10
|
<scrollview android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/htmlsource" /> </scrollview> |
界面如下:
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); pathtext = (edittext) this .findviewbyid(r.id.path); htmlsource = (textview) this .findviewbyid(r.id.htmlsource); button button = (button) this .findviewbyid(r.id.button); button.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { string path = pathtext.gettext().tostring(); try { //獲取源碼 string html = pageservice.gethtml(path); htmlsource.settext(html); } catch (exception e) { toast.maketext(getapplicationcontext(), r.string.error, 1 ).show(); } } }); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class pageservice { /** * 獲取網頁源代碼 * @param path 網頁路徑 * @return */ public static string gethtml(string path) throws exception{ url url = new url(path); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setconnecttimeout( 5000 ); conn.setrequestmethod( "get" ); if (conn.getresponsecode() == 200 ){ byte [] data = streamtool.read(conn.getinputstream()); return new string(data, "utf-8" ); } return null ; } } |
希望本文所述對大家android程序設計有所幫助。