本文介绍了Android BitmapFactory.decodeStream(...)不会在模拟器上加载HTTPS URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

App doesn't load an Image from an HTTPS URL when run on the emulator.

示例代码:

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

在实际设备上运行时,图像加载就很好.如果通过HTTP(而不是HTTPS)访问图像,则仿真器还将加载图像.

The image loads just fine when run on an actual device. Also the emulator loads the image if it's accessed via HTTP instead of HTTPS.

我做错了还是这是一个已知问题?

Am I doing something wrong or is this a known issue?

推荐答案

使用以下代码在URL中的imageview中显示图像.

Use below code for display image in imageview from url.

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);
}

这篇关于Android BitmapFactory.decodeStream(...)不会在模拟器上加载HTTPS URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:04