Android的FileNotFound例外 - 无法从没有文件格式的图像URL的getInputStream [英] Android FileNotFound Exception - Cannot getInputStream from image URL that does not have file format

查看:128
本文介绍了Android的FileNotFound例外 - 无法从没有文件格式的图像URL的getInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是pretty的自我解释。

The title is pretty self explanatory.

以下code ...:

the following code...:

    URL imageUrl = new URL(url);
   try {
                HttpURLConnection conn= (HttpURLConnection)imageUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                int length = conn.getContentLength();
                InputStream is = conn.getInputStream();
                return BitmapFactory.decodeStream(is);
           } catch (IOException e) {
            e.printStackTrace();
           }

将失败如果URL中不包含的文件格式。例如,由谷歌托管的一些图像将显示图像尚未有文件格式(巴纽,JPG格式,等等)作为URL的一部分。

Will fail if the url does not contain the file format. For example, some images hosted by google will display the image yet not have the file format (.png, .jpg, etc) as part of the url.

因此​​,URL连接返回text / html的的Content-Type头。我认为这是问题。

As a result, the content-type header of the url connection returns "text/html". I believe this is the issue.

我已经尝试设置一个新的内容类型标头,但似乎没有任何改变。

I have tried setting a new content-type header, but that doesnt seem to change anything.

有人知道一个解决办法?

anybody know a workaround?

感谢。

推荐答案

我遇到了问题,我记得谷歌搜索周围,这是我最后的解决办法

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

这篇关于Android的FileNotFound例外 - 无法从没有文件格式的图像URL的getInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆