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

 更新时间:2017年04月29日 10:19:37   作者:想今生得与失  
本篇文章主要介绍了Android 网络图片查看器与网页源码查看器的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧

在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实现仿网易新闻主界面设计的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android开发笔记之:Dialog的使用详解

    Android开发笔记之:Dialog的使用详解

    本篇文章是对Android中Dialog的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android实现日期时间选择对话框

    Android实现日期时间选择对话框

    这篇文章主要为大家详细介绍了Android实现日期以及时间选择对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Flutter获取ListView当前正在显示的Widget信息(应用场景)

    Flutter获取ListView当前正在显示的Widget信息(应用场景)

    ListView是Flutter里最常用的Widget了,当屏幕放不下的时候,它可以自带滚动功能,用法也很简单,本文通过实例代码给大家介绍Flutter获取ListView当前正在显示的Widget信息,感兴趣的朋友一起看看吧
    2022-05-05
  • MUI整合上拉下拉的写法

    MUI整合上拉下拉的写法

    在制作APP的时候下拉刷新和上拉加载几乎都是一起使用的,今天以MUI的写法为例给大家分享一下整合的写法。
    2017-11-11
  • Android studio 生成带Kotlin文档的实现方式

    Android studio 生成带Kotlin文档的实现方式

    这篇文章主要介绍了Android studio 生成带Kotlin文档的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 谈谈Android Fragments 详细使用

    谈谈Android Fragments 详细使用

    本篇文章主要介绍了Android Fragments 详细使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Android 中Activity 之间传递参数

    Android 中Activity 之间传递参数

    这篇文章主要介绍了Android 中Activity 之间传递参数的相关资料,希望通过本文能帮助到大家,让大家实现这样的方法,需要的朋友可以参考下
    2017-10-10
  • Android自定义View 实现水波纹动画引导效果

    Android自定义View 实现水波纹动画引导效果

    在android程序开发中,我们经常简单通过自定义view实现水波纹动画引导功能,下面通过本文给大家分享实现代码,需要的朋友参考下
    2017-01-01
  • Android编程之软件的安装和卸载方法

    Android编程之软件的安装和卸载方法

    这篇文章主要介绍了Android编程之软件的安装和卸载方法,涉及Android编程实现软件的安装、权限修改及卸载的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-12-12

最新评论