程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-加載遠程圖像到ImageView,但沒有加載上

android-加載遠程圖像到ImageView,但沒有加載上

編輯:編程綜合問答
加載遠程圖像到ImageView,但沒有加載上

我使用以下函數加載一個遠程圖像到ImageView。圖像存在於URL,我想從URL中加載圖像,但當我調用函數想顯示圖像時, ImageView仍是空白。為什麼會出現這種情況啊?

public static void setImage(ImageView view, String url) throws IOException { 

        final URLConnection conn = new URL(url).openConnection(); 
        conn.connect(); 
        final InputStream is = conn.getInputStream(); 

        final BufferedInputStream bis = new BufferedInputStream(is, 100000); 

        final Bitmap bm = BitmapFactory.decodeStream(bis); 
        bis.close(); 
        is.close(); 

        view.setImageBitmap(bm); 
    } 

最佳回答:


通過下面的代碼能夠加載遠程圖像到ImageView:

 Bitmap bitmap = null;

    HttpGet httpRequest = null;


    try {
            URL url = new URL(your_url);
            httpRequest = new HttpGet(url.toURI());

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
            InputStream instream = bufHttpEntity.getContent();
            bitmap = BitmapFactory.decodeStream(instream);
    } catch (URISyntaxException e) {
            e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved