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

Android实现网页图片浏览功能

程序员文章站 2023-12-02 22:22:10
本文实例为大家分享了andandroid实现网页图片浏览的具体代码,供大家参考,具体内容如下 基本功能:输入图片的url然后点击按钮加载出来图片 activity...

本文实例为大家分享了andandroid实现网页图片浏览的具体代码,供大家参考,具体内容如下

基本功能:输入图片的url然后点击按钮加载出来图片

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/activity_main" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="cn.edu.bzu.imageview.mainactivity"> 

 <imageview 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:id="@+id/iv" 
  android:layout_weight="1000" 
  /> 
 <edittext 
  android:id="@+id/et_path" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:hint="请输入图片的路径" 
  android:text="http://pic.58pic.com/58pic/16/62/63/97m58picywm_1024.jpg" 
  android:singleline="true" /> 
 <button 
  android:text="浏览" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:onclick="click"/> 
</linearlayout> 

mainactivity:

package cn.edu.bzu.imageview; 

import android.app.activity; 
import android.graphics.bitmap; 
import android.graphics.bitmapfactory; 
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.imageview; 
import android.widget.toast; 

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

public class mainactivity extends activity { 
 protected static final int change_ui = 1; 
 protected static final int error = 2; 
 private edittext et_path; 
 private imageview iv; 
 private handler handler = new handler() { 
  public void handlemessage(message msg) { 
   if (msg.what == change_ui) { 
    bitmap bitmap = (bitmap) msg.obj; 
    iv.setimagebitmap(bitmap); 

   } else if (msg.what == error) { 
    toast.maketext(mainactivity.this, "显示图片错误",toast.length_short).show(); 
   } 
  } 

  ; 
 }; 

 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
  et_path = (edittext) findviewbyid(r.id.et_path); 
  iv = (imageview) findviewbyid(r.id.iv); 
 } 

 public void click(view view) { 
  final string path = et_path.gettext().tostring().trim(); 
  if (textutils.isempty(path)) { 
   toast.maketext(this, "图片路径不能为空", toast.length_short).show(); 
  } else { 
//子线程请求网络,android4.0以后访问网络不能放在主线程中 
   new thread() { 
    private httpurlconnection conn; 
    private bitmap bitmap; 

    public void run() {//连接服务器get请求,获取图片 
     //创建url对象 
     try { 
      url url = new url(path); 
      conn = (httpurlconnection) url.openconnection();//根据url发送http的请求 
      conn.setrequestmethod("get");//设置请求的方式 
      conn.setconnecttimeout(5000);//设置超时时间 
      conn.setrequestproperty("user-agent", "mozilla/4.0(compatible;msie 6.0;windows nt 5.1;" + "sv1;.net4.0c;.net4.0e;.net clr 2.0.50727;" + ".net clr 3.0.4506.2152;.net clr 3.5.30729;shuame)");//设置浏览器的版本 
      int code = conn.getresponsecode();//得到服务器返回的响应码 
      if (code == 200) {//请求成功后返回码是200 
       inputstream is = conn.getinputstream(); 
       bitmap = bitmapfactory.decodestream(is); 
       message msg = new message(); 
       msg.what = change_ui; 
       msg.obj = bitmap; 
       handler.sendmessage(msg); 
      } else { 
       message msg = new message(); 
       msg.what = error; 
       handler.sendmessage(msg); 
      } 

     } catch (java.io.ioexception e) { 
      e.printstacktrace(); 
      message msg = new message(); 
      msg.what = error; 
      handler.sendmessage(msg); 
     } 
    } 
   }.start(); 
  } 
 } 

 这样基本功能就能实现了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。