本文介绍了Android的BitmapFactory.de codeStream(...)不加载在模拟器HTTPS URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应用程序不能在模拟器中运行时,从HTTPS URL加载图像。
App doesn't load an Image from an HTTPS URL when run on the emulator.
样品code:
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?
推荐答案
使用低于code为显示图像的ImageView的URL。
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);
,也可以使用下面code为。
And also use below code for that.
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.de codeStream(...)不加载在模拟器HTTPS URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!