xml

<?xml version="1.0"?>

-<LinearLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:id="@+id/et_url" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="http://www.baidu.com"/>

<Button android:id="@+id/bt_looksource" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="查看图片"/>

-<ScrollView android:layout_height="wrap_content" android:layout_width="wrap_content">

<ImageView android:id="@+id/img_pic" android:layout_height="match_parent" android:layout_width="match_parent"/>

</ScrollView>

</LinearLayout>

图片查看控件

java

package com.itheima.sourcelook;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.itheima.piclook.R; public class MainActivity extends Activity implements OnClickListener{ private EditText et_url;
private ImageView img_pic;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext =this;
et_url = (EditText) findViewById(R.id.et_url);
Button bt_looksource = (Button) findViewById(R.id.bt_looksource);
img_pic = (ImageView) findViewById(R.id.img_pic); //二.设置点击事件
bt_looksource.setOnClickListener(this); System.out.println("oncreate方法线程:"+Thread.currentThread().getName()); } //☆☆☆1.在主线程中创建一个Handler对象
private Handler handler = new Handler(){
//☆☆☆2.重写handler的handlermessage方法,用来接收子线程中发来的消息
public void handleMessage(android.os.Message msg) {
//☆☆☆5.接收子线程发送的数据,处理数据。
Bitmap bitmap = (Bitmap) msg.obj;
//☆☆☆6.当前方法属于主线程可以做UI的更新
//五.获取服务器返回的内容,显示到textview上
img_pic.setImageBitmap(bitmap);//设置ImageView的图片内容
};
}; @Override
public void onClick(View v) { try{
//三.oclick方法中获取用户输入的url地址
final String url_str = et_url.getText().toString().trim();
if(TextUtils.isEmpty(url_str)){
Toast.makeText(mContext, "url不能为空", 0).show();
return ;
} System.out.println("oclick方法线程:"+Thread.currentThread().getName()); //创建一个子线程做网络请求
new Thread(new Runnable() { @Override
public void run() {
try{
System.out.println("oclick方法runnable线程:"+Thread.currentThread().getName()); //四.请求url地址
//1.创建一个Url对象
URL url = new URL(url_str);
//2.获取一个UrlConnection对象
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//3.为UrlConnection对象设置一些请求的参数,请求方式,连接的超时时间
connection.setRequestMethod("GET");//设置请求方式
connection.setConnectTimeout(1000*10);//设置超时时间 //4.在获取url请求的数据前需要判断响应码,200 :成功,206:访问部分数据成功 300:跳转或重定向 400:错误 500:服务器异常
int code = connection.getResponseCode();
if(code == 200){
//5.获取有效数据,并将获取的流数据解析成String
InputStream inputStream = connection.getInputStream(); //将一个读取流转换成一个图片 Drawable , Btimap:位图 ?????
Bitmap bitmap = BitmapFactory.decodeStream(inputStream); //☆☆☆3.子线中创建一个Message对象,为了携带子线程中获取的数据给主线程。
Message msg = Message.obtain();//获取一个Message对象,内部实现是:如果之前的Message存在直接返回,不存在创建新的Message返回
msg.obj = bitmap;//将获取的数据封装到msg中。
//☆☆☆4.使用handler对象将message发送到主线程。
handler.sendMessage(msg); } }catch (Exception e) {
e.printStackTrace();
} }
}).start(); }catch (Exception e) {
e.printStackTrace();
}
} }

MainActivity

字节流操作

package com.itheima.sourcelook;

import java.io.ByteArrayOutputStream;
import java.io.InputStream; public class StreamUtils { public static String streamToString(InputStream in){
String result =""; try{
//创建一个字节数组写入流
ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024];
int length = 0;
while ( (length = in.read(buffer)) !=-1) {
out.write(buffer, 0, length);
out.flush();
} result = out.toString();//将字节流转换成string out.close();
}catch (Exception e) {
e.printStackTrace();
} return result;
}
}

StreamUtils

老师笔记

04  网络图片查看器

adb shell+  input text 内容;可以通过将内容输入到手机上的输入框。

将一个读取流转换成bitmap对象:

BitmapFactory:可以将文件,读取流,字节数组转换成一个Bitmap对象。
        Bitmap bitmap = BitmapFactory.decodeStream(InputStream in);
        
        imageView.setImageBitmap(bitmap);//设置图片内容

android 网络_网络图片查看器的更多相关文章

  1. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  2. Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯

    1. 原理图 2. 示例代码 (网络图片查看器) (1)  HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...

  3. Android 网络图片查看器

    今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...

  4. android 网络_网络源码查看器

    xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

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

    在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...

  6. Android中的图片查看器

    本案例,使用Eclipse来开发Android2.1版本号的图片查看器. 1)首先,打开Eclipse.新建一个Android2.1版本号的项目ShowTu,打开res/values中文件夹下的str ...

  7. Android简易实战教程--第二十六话《网络图片查看器在本地缓存》

    本篇接第二十五话  点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...

  8. Android简易实战教程--第二十五话《网络图片查看器》

    访问网络已经有了很成熟的框架.这一篇只是介绍一下HttpURLConnection的简单用法,以及里面的"注意点".这一篇可以复习或者学习HttpURLConnection.han ...

  9. android网络图片查看器

    package com.itheima.netimageviewer; import java.io.BufferedReader; import java.io.File; import java. ...

随机推荐

  1. Android创建桌面快捷方式

    在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名.在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setCla ...

  2. WebService究竟是什么?

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间非常多计算机期刊.书籍和站点都大肆的提及和宣传WebService技术,当中不乏非常多吹嘘和做广告的成分.可是不得不承认的是W ...

  3. 对javascript this的理解

    对于this的理解,大部分时间都比较模糊,最近几天做了一些研究,记录一下 首先应该明白,this是执行上下文的一个属性,它的值取决于执行上下文,执行上下文和函数调用方式相关,定义一个function的 ...

  4. j简单的递归

    1 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有的信都装错信封共有多少种不同情况. 归纳法例子 1.有n个硬币(n为偶数)正面朝上排成一排,每次将n-1个硬币翻成朝上为止.编程让计算机把 ...

  5. 快速排序算法-C语言实现

    注:本篇内容为翻译,之所以选择这篇进行翻译原因是该文章含有动画,能够更加直观地展示快速排序.同时,可以仔细看一下代码,代码中把结构化的思想给予了更加充分地表现.按照功能进行模块划分的思想得到了彻底地贯 ...

  6. windows获取时间的方法

    介绍       我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录 ...

  7. 【AsyncTask整理 1】 AsyncTask几点要注意的地方

    问题1:AsyncTask是多线程吗? 答:是. 问题2:AsyncTask与Handler相比,谁更轻量级? 答:通过看源码,发现AsyncTask实际上就是一个线程池,而网上的说法是AsyncTa ...

  8. 使用SFTP上传文件到服务器的简单使用

    最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询 /** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp ...

  9. Oracle基础—表分区

    一:表分区的应用场景 用于管理包含大量数据的表. 二:表分区的优点 1.提高数据的可以性 2.减少管理负担 3.改善语句的性能 三:分区的方式:(区间分区.散列分区.列表分区.组合分区) 1.区间分区 ...

  10. Flume Spooldir 源的一些问题

    Flume Spooldir 源的一些问题 来自:http://blog.xlvector.net/2014-01/flume-spooldir-source-problem/ ( 自己写的插件,数据 ...