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

tam*_*ler 2 android

在模拟器上运行时,应用程序不会从HTTPS URL加载图像.

示例代码:

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

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

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

Dip*_*iya 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);
Run Code Online (Sandbox Code Playgroud)

并且还使用下面的代码.

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 上面的第二个解决方案有效 非常感谢你. (2认同)