Android的FileNotFound例外 - 无法从没有文件格式的图像URL的getInputStream文件格式、图像、FileNotFound、Android

由网友(倒也安稳)分享简介:标题是pretty的自我解释。The title is pretty self explanatory.以下code ...:the following code...:URL imageUrl = new URL(url);try {HttpURLConnection conn= (HttpURLConnect...

标题是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);
        }
阅读全文

相关推荐

最新文章