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

发布时间 - 2026-01-11 00:53:59    点击率:

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

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

效果图如下:

下面是主要代码:

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

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

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

主要代码:

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

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


# android源码查看器  # 网页源码查看器  # android  # 网络图片查看器  # android查看网络图片的实现方法  # Android图片处理教程之全景查看效果实现  # Android仿百度图片查看功能  # Android 简单的图片查看器源码实现  # android自定义Camera拍照并查看图片  # Android 通过网络图片路径查看图片实例详解  # android网络图片查看器简单实现代码  # Android 实现WebView点击图片查看大图列表及图片保存功能  # Android实现图片查看功能  # 请输入  # 这是  # 所示  # 看一下  # 涉及到  # 必不可少  # 先看  # 权限访问  # 为空  # 查看器  # protected  # void  # savedInstanceState  # super  # setContentView  # onCreate  # et_input  # String  # private  # iv_show 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何基于云服务器快速搭建网站及云盘系统?  深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?  ,交易猫的商品怎么发布到网站上去?  Laravel如何实现API版本控制_Laravel版本化API设计方案  Laravel中间件起什么作用_Laravel Middleware请求生命周期与自定义详解  php 三元运算符实例详细介绍  EditPlus中的正则表达式 实战(1)  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  音乐网站服务器如何优化API响应速度?  Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】  详解Android中Activity的四大启动模式实验简述  如何在建站主机中优化服务器配置?  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  Laravel如何为API生成Swagger或OpenAPI文档  Laravel路由Route怎么设置_Laravel基础路由定义与参数传递规则【详解】  想要更高端的建设网站,这些原则一定要坚持!  Laravel如何实现文件上传和存储?(本地与S3配置)  JS中页面与页面之间超链接跳转中文乱码问题的解决办法  android nfc常用标签读取总结  Laravel如何使用Blade模板引擎?(完整语法和示例)  智能起名网站制作软件有哪些,制作logo的软件?  英语简历制作免费网站推荐,如何将简历翻译成英文?  Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】  如何在新浪SAE免费搭建个人博客?  如何在万网主机上快速搭建网站?  如何在HTML表单中获取用户输入并结合JavaScript动态控制复利计算循环  佛山网站制作系统,佛山企业变更地址网上办理步骤?  Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  html5源代码发行怎么设置权限_访问权限控制方法与实践【指南】  Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】  利用python获取某年中每个月的第一天和最后一天  如何利用DOS批处理实现定时关机操作详解  微信h5制作网站有哪些,免费微信H5页面制作工具?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像  ,在苏州找工作,上哪个网站比较好?  Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】  iOS中将个别页面强制横屏其他页面竖屏  如何破解联通资金短缺导致的基站建设难题?  Laravel如何操作JSON类型的数据库字段?(Eloquent示例)  Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】  如何在企业微信快速生成手机电脑官网?  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  高防服务器如何保障网站安全无虞?  北京网站制作公司哪家好一点,北京租房网站有哪些?  Laravel怎么使用Intervention Image库处理图片上传和缩放  佛山企业网站制作公司有哪些,沟通100网上服务官网?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?