将输入流转换为位图

发布于 2024-11-19 02:03:40 字数 408 浏览 1 评论 0原文

我在将输入流从网络转换为位图时遇到问题。仅当输入图像类型为 .BMP(位图)时才会出现问题。在这种情况下:bitmapFactory.decodeStream 返回 null

有任何提示如何解决此问题或我应该在哪里继续调试?

平台:Android(蜂巢)

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

I have problems converting a input stream from web into bitmap. Problem occurs only when input image type is .BMP (bitmap). In that case: bitmapFactory.decodeStream returns null.

Any hints how to fix this problem or where should I continue my debugging?

Platform: Android (Honeycomb)

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2024-11-26 02:03:40

谢谢@Amir 指出日志。发现了一行:

decoder->decode returned false

这似乎是一个常见问题。经过搜索,我找到了解决方案。

我以前的代码:

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

正在运行的代码:

HttpGet httpRequest = null;

try {
    httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStream instream = bufHttpEntity.getContent();

bmp = BitmapFactory.decodeStream(instream);

来源

Thank you @Amir for point out the log. Discovered a line:

decoder->decode returned false

This seems to be a common problem. Doing a search I found a solution.

My previous code:

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

Code which is working:

HttpGet httpRequest = null;

try {
    httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStream instream = bufHttpEntity.getContent();

bmp = BitmapFactory.decodeStream(instream);

Source

南巷近海 2024-11-26 02:03:40

这是一个单行答案

val bitmap = BitmapFactory.decodeStream(inputStream)

返回一个位图

Here is a one-line answer

val bitmap = BitmapFactory.decodeStream(inputStream)

Returns a Bitmap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文