问题描述
应用程序在仿真器上运行时不会从HTTPS URL加载图像.
示例代码:
URL url = new URL("https://someserver.com/photo.jpg"); mImageView.setImageBitmap(BitmapFactory.decodeStream(url.openStream()));
在实际设备上运行时,图像加载即可.如果通过HTTP而不是HTTPS访问,则仿真器也加载图像.
我做错了什么,或者是一个已知的问题吗?
推荐答案
在imageview中使用以下代码从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); }
其他推荐答案
是一个可信的https网站吗?如果不是,你会有一个连接的问题.
看看这个...
http://droidos- coding.blogspot.com/2012/03/android-trusting-all-https.c-signed.html
其他推荐答案
试用此代码:
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; } }
和请确保您在清单文件中添加了 Internet权限. 这将有助于你这样做.
问题描述
App doesn't load an Image from an HTTPS URL when run on the emulator.
Sample code:
URL url = new URL("https://someserver.com/photo.jpg"); mImageView.setImageBitmap(BitmapFactory.decodeStream(url.openStream()));
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?
推荐答案
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);
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); }
其他推荐答案
Is this a trusted https site? If not you will have a problem with the connection.
Take a look at this...
http://droidos-coding.blogspot.com/2012/03/android-trusting-all-https-self-signed.html
其他推荐答案
Try this code:
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; } }
And pls make sure u have added internet permission in your manifest file. This will help u to do so.