美文网首页
两种方式实现网络图片查看器

两种方式实现网络图片查看器

作者: Maybe_G | 来源:发表于2017-11-21 12:44 被阅读0次

    分别采用两种方法实现网络图片查看器。
    效果如图所示。

    微信截图_20171121124018.png
    • 01 在子线程里面执行耗时操作,在主线程更新UI
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private Button button;
        private ImageView imageView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button)findViewById(R.id.bt_view);
            imageView = (ImageView)findViewById(R.id.image_view);
            button.setOnClickListener(this);
    
        }
    
        //UI只能在主线程更新,利用Handler来将更新UI的操作从子线程切换到主线程上,即将获取的图片显示在屏幕上
        Handler handler = new Handler(){
            public void handleMessage(Message msg){
                Bitmap bitmap = (Bitmap)msg.obj;
                if(bitmap == null){
                    Toast.makeText(MainActivity.this,"获取失败...",Toast.LENGTH_SHORT).show();
                }else{
    
                    imageView.setImageBitmap(bitmap);
                }
            }
        };
    
        //button点击监听,创建一个子线程处理连接网络获取图片的耗时操作
        public void onClick(View v){
            final String path = "http://192.168.107.2:8080/pro2.png";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //获取Bitmap类型的图片信息
                    Bitmap bitmap = getImage(path);
                    Message msg = new Message();
                    //向Handler传递图片信息
                    msg.obj = bitmap;
                    handler.sendMessage(msg);
                }
            }).start();
        }
        //获取图片数据
        public Bitmap getImage(String path){
            try {
                //2.获取URL对象
                URL url = new URL(path);
                //3.获取连接对象
                final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                //4.初始化
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                conn.connect();
                //如果conn.getResponseCode()==200,则表示连接成功
                if(conn.getResponseCode() == 200){
                    //获取图片数据流
                    InputStream is = conn.getInputStream();
                    //BitmapFactory加工成Bitmap格式
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
    
            }
            return null;
        }
    }
    
    • 02 使用异步机制AsyncTask
    public class MainActivity extends AppCompatActivity{
    
        private Button button;
        private ImageView imageView;
        private GetNetPic getPic;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button)findViewById(R.id.bt_view);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String path = "http://192.168.107.2:8080/pro2.png";
                    getPic = new GetNetPic();
                    //这里是调用execute()方法,触发异步任务的执行
                    getPic.execute(path);
                }
            });
            imageView = (ImageView)findViewById(R.id.image_view);
        }
    
        class GetNetPic extends AsyncTask<String,Bitmap,Bitmap>{
            @Override
            protected Bitmap doInBackground(String... params) {
                try {
                    //2.获取URL对象
                    URL url = new URL(params[0]);
                    Log.d("GetNetPic","params[0] = " + params[0]);
                    //3.获取连接对象
                    final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                    //4.初始化
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    conn.connect();
                    //如果conn.getResponseCode()==200,则表示连接成功
                    if(conn.getResponseCode() == 200){
                        Log.d("GetNetPic","OK");
                        //获取图片数据流
                        InputStream is = conn.getInputStream();
                        //BitmapFactory加工成Bitmap格式
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        return bitmap;
                    }else {
                        Log.d("GetNetPic","Not OK");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
    
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Bitmap bitmap) {
    
                super.onPostExecute(bitmap);
                if(bitmap == null){
                    Toast.makeText(MainActivity.this,"获取失败...",Toast.LENGTH_SHORT).show();
                }else{
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:两种方式实现网络图片查看器

          本文链接:https://www.haomeiwen.com/subject/ckoivxtx.html