Android:bitmapfactory.decodestream 返回 null [英] Android: bitmapfactory.decodestream returns null

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

问题描述

我试图从图像的路径中获取位图图像.但是 BitmapFactory.decodeStream 返回 null 值.

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

代码:

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

问题在于,一旦您使用了 HttpUrlConnection 中的 InputStream,就无法倒带并再次使用相同的 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.

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

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