Android FileNotFound Exception - Cannot getInputStream from image URL that does not have file format

后端 未结 2 1652
星月不相逢
星月不相逢 2020-12-11 09:24

The title is pretty self explanatory.

the following code...:

    URL imageUrl = new URL(url);
   try {
                HttpURLConnection conn= (HttpU         


        
相关标签:
2条回答
  • 2020-12-11 09:47

    I was running into problems and I remembered googling around and this was my final solution

            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 bufHttpEntity = new BufferedHttpEntity(entity);
                InputStream input = bufHttpEntity.getContent();
    
                Bitmap bitmap = BitmapFactory.decodeStream(input);
    
                ImageActivity.this.i.setImageBitmap(bitmap);
                ImageActivity.this.i.refreshDrawableState();
                input.close();
    
            } catch (MalformedURLException e) {
                Log.e("ImageActivity", "bad url", e);
            } catch (Exception e) {
                Log.e("ImageActivity", "io error", e);
            }
    
    0 讨论(0)
  • 2020-12-11 10:06

    BitmapFactory.decodeStream only reads the raw byte stream of the image itself, it has no knowledge about the URL nor the Content Encoding, so I don't think they are at fault here. It should be able to read the image format directly from the image header.

    Probably the URL gets redirected.

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