Android BitmapFactory.decodeStream(...)不会在模拟器上加载HTTPS URL

时间:2012-07-07 06:29:40

标签: android

在模拟器上运行时,App无法从HTTPS URL加载图像。

示例代码:

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

在实际设备上运行时,图像加载得很好。如果图像通过HTTP而不是HTTPS访问,模拟器也会加载图像。

我做错了什么或这是一个已知问题?

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

这是一个受信任的https网站吗?如果不是,您将遇到连接问题。

看看这个......

http://droidos-coding.blogspot.com/2012/03/android-trusting-all-https-self-signed.html

答案 2 :(得分: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;
           }
         }

请确保您已在清单文件中添加互联网权限。 这将有助于你这样做。