服务器之家

服务器之家 > 正文

Android编程实现网络图片查看器和网页源码查看器实例

时间:2021-05-03 15:29     来源/作者:傅荣康

本文实例讲述了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程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部