Android网络图片查看器与网页源码查看器
在AndroidManifest.xml里面先添加权限访问网络的权限:
创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕十载,专业且经验丰富。十载网站优化营销经验,我们已为上1000家中小企业提供了成都做网站、成都网站设计解决方案,按需搭建网站,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!
效果图如下:
下面是主要代码:
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的布局:
源码: 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布局:
源码: http://pan.baidu.com/s/1bp6EwyF
http://pan.baidu.com/s/1c2H1JlI
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持创新互联!
分享名称:Android网络图片查看器与网页源码查看器
文章起源:http://hbruida.cn/article/geocgs.html