Android:bitmapfactory.decodestream返回null

前端之家收集整理的这篇文章主要介绍了Android:bitmapfactory.decodestream返回null前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从图像的路径获取位图图像.但BitmapFactory.decodeStream返回null值.

码:

URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
input.close();

我在更多的网站上搜索过,但我还没有得到解决方案.

解决方法

得到了解决方案:-)
HttpGet httpRequest = new HttpGet(URI.create(path) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();

问题是,一旦你使用了来自HttpUrlConnection的InputStream,你就不能再回放并再次使用相同的InputStream.因此,您必须为图像的实际采样创建新的InputStream.否则我们必须中止http请求.

猜你在找的Android相关文章