Android的FileNotFound例外 - 无法从的getInputStream图像URL不具有文件格式
-
26-09-2019 - |
题
该标题是相当自我解释。
下面的代码...
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中不包含的文件格式失败。例如,通过谷歌托管某些图像将显示图像还没有文件格式(png格式,.JPG,等)作为URL的一部分。
其结果是,该URL的内容类型报头连接返回“text / html的”。我认为这是问题。
我试图设置一个新的内容类型报头,但似乎不改变任何东西。
有人知道一个解决办法:
感谢。
解决方案
我跑成问题,我想起谷歌搜索周围,这是我的最终溶液
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);
}
其他提示
BitmapFactory.decodeStream
只读取图像的原始字节流本身,它不知道该URL也不内容编码知识,所以我不认为自己做错了这里。它应该能够直接从图像标题中读取的图像格式。
可能的URL被重定向。
不隶属于 StackOverflow