2012-07-07 46 views
2

在仿真器上運行時,應用程序不會從HTTPS URL加載圖像。Android BitmapFactory.decodeStream(...)不會在仿真器上加載HTTPS URL

示例代碼:

URL url = new URL("https://someserver.com/photo.jpg"); 
mImageView.setImageBitmap(BitmapFactory.decodeStream(url.openStream())); 

圖像加載就好當實際設備上運行。如果通過HTTP而不是HTTPS訪問圖像,模擬器也會加載圖像。

我做錯了什麼或者這是一個已知的問題?

+0

試試這個: 位圖位= BitmapFactory.decodeStream((InputStream的)新的網址(URL).getContent()); mImageView.setImageBitmap(bitmap); – AkashG 2012-07-07 06:36:42

+0

@AkashG同樣的結果。與HTTPS一起使用時不加載圖像。 – tamsler 2012-07-07 06:40:24

+0

@tamsler檢查答案在http://stackoverflow.com/questions/11241137/inputstream-read-has-no-response-when-downloading-large-imagesize-300k/11241660#11241660 – Khan 2012-07-07 07:04:44

回答

7

使用以下代碼顯示來自url的imageview中的圖像。

ImageView mImageView = (ImageView)findViewById(R.id.mImageView1); 

URL url = new URL(address); 
InputStream content = (InputStream)url.getContent(); 
Drawable d = Drawable.createFromStream(content , "src"); 
mImageView.setImageDrawable(d); 

而且還使用下面的代碼。

try { 
    URL url = new URL(imageUrl); 
    HttpGet httpRequest = null; 

    httpRequest = new HttpGet(url.toURI()); 

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

    HttpEntity entity = response.getEntity(); 
    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity); 
    InputStream input = b_entity.getContent(); 

    Bitmap bitmap = BitmapFactory.decodeStream(input); 

    ImageView mImageView = (ImageView) findViewById(R.id.mImageView); 
    mImageView.setImageBitmap(bitmap); 
} catch (MalformedURLException e) { 
    Log.e("log", "bad url", t); 
} catch (IOException e) { 
    Log.e("log", "io error", t); 
} 
+2

。非常感謝你。 – tamsler 2012-07-08 02:02:17

+0

謝謝...它爲我工作 – Noman 2013-11-18 08:50:32

+0

第二個也爲我工作,謝謝! – 2014-09-13 23:32:12

0

試試這個代碼:

imageView.setImageBitmap(LoadImageFromWebOperations(url)); 

private Bitmap LoadImageFromWebOperations(String url){ 
      try{ 
      String encodedurl = url.replace(" ", "%20"); 
      InputStream is = (InputStream) new URL(encodedurl).getContent(); 
      Bitmap d = BitmapFactory.decodeStream(is); 
      return d; 
      }catch (Exception e) { 
      e.printStackTrace(); 
//   System.out.println("Exc="+e); 
      return null; 
      } 
     } 

和PLS確保ü已經在你的清單文件添加Internet權限。 這將幫助你這樣做。

+0

我剛試過你的解決方案,沒有運氣。這似乎是AVD(模擬器)Android v2.2的問題。這些圖像在基於4.x的仿真器以及物理設備上正確顯示。上面的第二個解決方案的工作原理是 – tamsler 2012-07-08 01:54:50