zoukankan      html  css  js  c++  java
  • 安卓开发之图片查看器

    package com.lidaochen.test;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    public class MainActivity extends AppCompatActivity {
        private EditText et_path;
        private ImageView iv_pic;
    
        // 创建handler对象
        public Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bitmap bitmap = (Bitmap)msg.obj;
                // 设置图片到ImageView
                iv_pic.setImageBitmap(bitmap);
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 找到  ImageView 和 EditText控件
            et_path = (EditText)findViewById(R.id.et_path);
            iv_pic = (ImageView)findViewById(R.id.iv_pic);
        }
        public void click(View v)
        {
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        // 获取图片路径
                        String path = et_path.getText().toString().trim();
                        // 创建url对象
                        URL url = new URL(path);
                        //  获取HttpURLConnection对象
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                        // 设置请求方式
                        httpURLConnection.setRequestMethod("GET");
                        // 设置超时时间
                        httpURLConnection.setReadTimeout(5000);
                        // 获取服务器返回的状态码
                        int code = httpURLConnection.getResponseCode();
                        if (code == 200)
                        {
                            // 获取图片数据,不管什么数据,都是以流的形式返回
                            InputStream in = httpURLConnection.getInputStream();
                            // 通过位图工厂,获取位图
                            final Bitmap bitmap = BitmapFactory.decodeStream(in);
                            // 创建MSG 对象
                            Message msg = new Message();
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
  • 相关阅读:
    JS---元素属性的操作
    JS---异常
    JS---OOP
    T-SQL---分页语句
    mui---在关闭webview之前给出提示
    mui---通过plus.webview.create创建webview并打开新页面并传参到新页面
    mui---要打开的页面loaded不自动显示,等服务器返回数据后,再做处理逻辑
    winform/timer控件/权限设置/三级联动
    winform 进程、线程
    winform 之MDI容器
  • 原文地址:https://www.cnblogs.com/duxie/p/10946683.html
Copyright © 2011-2022 走看看