激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - Android - Android編程實現網絡圖片查看器和網頁源碼查看器實例

Android編程實現網絡圖片查看器和網頁源碼查看器實例

2021-05-03 15:29傅榮康 Android

這篇文章主要介紹了Android編程實現網絡圖片查看器和網頁源碼查看器,結合實例形式分析了Android針對網絡圖片及網頁的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了android編程實現網絡圖片查看器和網頁源碼查看器。分享給大家供大家參考,具體如下:

網絡圖片查看器

清單文加入網絡訪問權限:

?
1
2
<!-- 訪問internet權限 -->
<uses-permission android:name="android.permission.internet"/>

界面如下:

 Android編程實現網絡圖片查看器和網頁源碼查看器實例

示例:

?
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>

界面如下:

 Android編程實現網絡圖片查看器和網頁源碼查看器實例

示例

?
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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
783
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 香蕉成人在线视频 | www.99re14.com| 粉嫩粉嫩一区二区三区在线播放 | 91 在线视频观看 | 九九热在线免费观看视频 | 国产精品啪一品二区三区粉嫩 | 亚洲三区精品 | 91免费无限观看 | 日本一区二区视频在线 | 久久国产精品99国产 | 黄色视频一级毛片 | 性色av一区二区三区四区 | 成年毛片 | 国产九色在线观看 | 国产在线精品一区二区夜色 | 国产精品久久久久一区二区 | 免费在线观看国产精品 | 久久精品视频12 | 欧美特级黄色 | 性欧美在线视频 | 女人一级一级毛片 | 国产精品久久99精品毛片三a | 午夜视| 欧美视频一二三区 | 欧美成人精品一区 | 亚洲欧美日韩中文在线 | 欧美性色大片 | 制服丝袜成人动漫 | 黄色成人短视频 | 免费在线观看国产精品 | 久久久久久免费免费 | 欧美成人精品一级 | 91久久久久久久一区二区 | 日本68xxxx| 99久久超碰中文字幕伊人 | 欧美激情首页 | 欧美视频99 | 欧美一级毛片一级毛片 | 欧美一级毛片特黄黄 | 在线观看中文字幕国产 | 99精品视频一区二区三区 |