android 网络_网络图片查看器

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);//设置图片内容

时间: 2024-08-07 04:34:00

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

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

1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调用getContentResolver().notifyChange(uri, null)来通知注册在此URI上的访问者,例子如下: private static final Uri URI = Uri.parse("content://person.db"); public class

【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目

备份短信和添加短信 操作系统短信的uri: content://sms/ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.backupsms" android:versionCode="1

(十二)网络图片查看器(注释更全面)

(一)android:layout_weight 在不同情况下的意义. 当 android:layout_width  和 android:layout_height都不为0的时候,android:layout_weight代表的是控件渲染的优先级,值越大,渲染的优先级越低.默认android:layout_weight=0. 当 android:layout_width  或 android:layout_height为0的时候,android:layout_weight才代表权重,值越大,权

android---利用android-async-http开源项目实现网络图片查看器

1.   导包:导入android-async-http开源项目的最新版本的包 2.简单的搭建一个网络图片查看器的界面 相关的界面搭建代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent

Android中的图片查看器

本案例,使用Eclipse来开发Android2.1版本的图片查看器. 1)首先,打开Eclipse,新建一个Android2.1版本的项目ShowTu,打开res/values中目录下的strings.xml,将其中代码替换成一下代码: 路径:ShowTu/res/valus/string.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="

[android] 网络图片查看器

界面布局LinerLayout线性布局,ImageView控件,EditText控件 hint属性提示信息,Button控件. Android:layout_weight=””属性,权重,只有控件的宽度和高度为0的时候才代表权重,否则它代表渲染的优先级,值越大优先级越低,默认是0,先渲染其他控件 singleLine属性 单行 业务逻辑,获取EditText的值放到ImageView里,实质上是http的get请求 获取EditText对象,通过findViewById() 获取值,通过调用Ed

android网络图片查看器

package com.itheima.netimageviewer; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; imp

Android smartimageview网络图片查看器

调用代码: SmartImageView siv = (SmartImageView) findViewById(R.id.siv);siv.setImageUrl(et_path.getText().toString().trim(),R.drawable.iclaunch,R.drawable.iclaunch);//参数分别为图片URL地址,加载失败时显示的图片,加载过程中显示的图片 开源代码查找方法: 在百度搜索栏中输入:csdn github android 开源代码

NetAnalyzer笔记 之 六 用C#打造自己的网络连接进程查看器(为进程抓包做准备)

[创建时间:2016-04-13 22:37:00] NetAnalyzer下载地址 起因 最近因为NetAnalyzer2016的发布,好多人都提出是否可以在NetAnalyzer中加入一个基于进程的抓包功能.所以最近研究了一下大概有这样一个想法: 获取系统打开端口的所有进程,并获取进程所开启的端口.IP地址以及使用的协议等信息,然后生成对应的过滤表达式,然后给NetAnalyzer设置该过滤表达式,然后开始抓包. 开始 虽然.Net中提供了进程信息查看的相关库,但是却没有提供相关网络端口查询