服务器之家:专注于服务器技术及软件下载分享
分类导航

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程序设计有所帮助。

延伸 · 阅读

精彩推荐