2012-07-07 33 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