安卓:bitmapfactory.de codestream返回null [英] Android: bitmapfactory.decodestream returns null

查看:119
本文介绍了安卓:bitmapfactory.de codestream返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从图像的路径得到的位图图像。但 BitmapFactory.de codeStream 返回值。

I have tried to get the bitmap image from the path of the image. But BitmapFactory.decodeStream returns null value.

code:

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

我有搜索在更多的网站,我仍然没有得到解决。

I have searched in more sites, still i did not get the solution.

推荐答案

得到了一个解决方案: - )

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

但问题是,一旦你使用一个InputStream从HttpURLConnection类,你不能倒回,并再次使用相同的InputStream。因此,你必须创建一个新的InputStream为图像的实际采样。否则,我们不得不放弃了http请求。

The problem was that once you've used an InputStream from a HttpUrlConnection, you can't rewind and use the same InputStream again. Therefore you have to create a new InputStream for the actual sampling of the image. Otherwise we have to abort the http request.

这篇关于安卓:bitmapfactory.de codestream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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