欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 网络图片查看器与网页源码查看器

程序员文章站 2023-11-24 18:11:40
在androidmanifest.xml里面先添加权限访问网络的权限:

在androidmanifest.xml里面先添加权限访问网络的权限:

<uses-permission android:name="android.permission.internet"/>

效果图如下:

Android 网络图片查看器与网页源码查看器

下面是主要代码:

package com.hb.neting;

import java.io.inputstream;
import java.net.httpurlconnection;
import java.net.url;

import android.annotation.suppresslint;
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.bundle;
import android.text.textutils;
import android.view.view;
import android.widget.edittext;
import android.widget.imageview;
import android.widget.toast;

public class mainactivity extends activity {
 private imageview iv_show;
 private edittext et_input;
 private string path;
 private int code;
 private httpurlconnection conn;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 iv_show=(imageview) findviewbyid(r.id.iv_show);
 et_input=(edittext) findviewbyid(r.id.et_inpput);
 }
 @suppresslint("showtoast") public void chakan(view view){
 path = et_input.gettext().tostring().trim();
 if (textutils.isempty(path)) {
 toast.maketext(mainactivity.this, "不能输入空的", 0).show();
 return;
 }
 new thread(){
 public void run() {
 try {
  url url = new url(path);
  conn = (httpurlconnection) url.openconnection();
  conn.setrequestmethod("get");
  conn.setconnecttimeout(5000);
  code = conn.getresponsecode();
  if(code==200){
  inputstream in = conn.getinputstream();
  //解析图片
  final bitmap stream = bitmapfactory.decodestream(in);
  runonuithread(new runnable() {
  public void run() {
  //更新ui
  iv_show.setimagebitmap(stream);
  }
  });
  in.close();
  }
 } catch (exception e) {
  e.printstacktrace();
 }
 };
 }.start();
 }
}

这是xml的布局:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <edittext
 android:id="@+id/et_inpput"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="请输入获取图片的地址:" />
 <button 
 android:id="@+id/bt_read"
 android:onclick="chakan"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="查看"
 />
 <imageview
 android:id="@+id/iv_show"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 />
</linearlayout>

源码: http://pan.baidu.com/s/1bp6ewyf

接着看一下网页源码查看器的小案例:

既然都涉及到网络的添加一个如上的网络权限是必不可少的了,具体操做如上所示,先看效果图:

Android 网络图片查看器与网页源码查看器

主要代码:

package com.hb.network;

import java.io.inputstream;
import java.net.httpurlconnection;
import java.net.url;

import android.annotation.suppresslint;
import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.text.textutils;
import android.view.view;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;

import com.hb.utils.readstreamutils;

public class mainactivity extends activity {
 protected static final int sucess = 0;
 protected static final int eorr = 1;
 private textview tv_show; 
 private edittext et_input;
 private url url;
 private string path;
 @suppresslint("handlerleak") 
 private handler handler=new handler(){
 public void handlemessage(android.os.message msg) {
 switch (msg.what) {
 case sucess:
 string content=(string) msg.obj;
 tv_show.settext(content);
 break;

 case eorr:
 toast.maketext(mainactivity.this,"查看源码失败" , 0).show();
 break;
 }
 };
 };
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 tv_show=(textview) findviewbyid(r.id.tv_show);
 et_input=(edittext) findviewbyid(r.id.et_input);

 }
 public void onclick(view view){
 path = et_input.gettext().tostring().trim();
 if(textutils.isempty(path)){
 return;
 }new thread(){
 public void run() {
 try {
  url = new url(path);
  //判断从edittext获取的数据否为空
  if(textutils.isempty(path)){
  return;
  }
  httpurlconnection conn = (httpurlconnection) url.openconnection();
  conn.setconnecttimeout(3000);
  conn.setrequestmethod("get");
  int code = conn.getresponsecode();
  if(code == 200){
  inputstream is= conn.getinputstream();
  string content = readstreamutils.read(is);
  message msg = new message();
  msg.what=sucess;
  msg.obj=content;
  handler.sendmessage(msg);
  }
 } catch (exception e) {
  e.printstacktrace();
  message msg = new message();
  msg.what=eorr;
  handler.sendmessage(msg);
 }
 };
 }.start();
 }
}
package com.hb.utils;

import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;

public class readstreamutils {
/**
 * 读取流的输入
 * @param is
 * @return
 * @throws ioexception
 */
 public static string read(inputstream is) throws ioexception{
 bytearrayoutputstream bos = new bytearrayoutputstream();
 int len;
 byte [] buffer=new byte[1024];
 while((len=is.read(buffer))!=-1){
 bos.write(buffer,0,len);
 }
 is.close();
 bos.close();
 string temp = bos.tostring();
 if(temp.contains("charset=utf-8")){
 return bos.tostring("utf-8");
 }else if(temp.contains("charset=iso-8859-1")){
 return bos.tostring("iso-8859-1");
 }
 return null;

 }
}

及xml布局:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="${relativepackage}.${activityclass}" >

 <edittext
 android:id="@+id/et_input"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="请输入要查看源码的网址:" />

 <button
 android:onclick="onclick"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="查看"
 android:textsize="25sp" />

 <scrollview
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

 <textview
 android:id="@+id/tv_show"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
 </scrollview>
</linearlayout>

源码: http://pan.baidu.com/s/1bp6ewyf

         http://pan.baidu.com/s/1c2h1jli

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!