Android BitmapFactory.decodeStream(…) doesn't load HTTPS URL on emulator

前端 未结 3 1707
清歌不尽
清歌不尽 2020-12-18 14:28

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\");
mImageV         


        
相关标签:
3条回答
  • 2020-12-18 14:59

    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

    0 讨论(0)
  • 2020-12-18 15:01

    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);
    }
    
    0 讨论(0)
  • 2020-12-18 15:10

    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.

    0 讨论(0)
提交回复
热议问题