BitmapFactory 解析網路請求的inputstream

weixin_34413357發表於2017-02-12
new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                
                try {
                    URL url = new URL("http://i4.pdim.gs/dmfd/200_200_100/t01c68b8a60e949ca92.png");
                    
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("GET");
                    con.setReadTimeout(2000);
                    con.setConnectTimeout(2000);
                    con.connect();
                    InputStream inputStream = con.getInputStream();
                    //轉化成圖片
                    final Bitmap map = BitmapFactory.decodeStream(inputStream);
                                          inputStream.close();
                    //設定
                    runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            iv.setImageBitmap(map);
                        }
                    });
                    
                    if (con.getResponseCode() == 200) {
                        
                        Log.e("dd", con.getResponseCode()+"");
                    }else {
                        
                        Log.e("dd","haha");
                    }
                    
                    
                    System.out.println(map.toString());
                    
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Log.e("cc", e.toString());
                    }
                
            }
        }).start();

相關文章