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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...