Java 将输入流转换为位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6612263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:15:54  来源:igfitidea点击:

Converting input stream into bitmap

javaandroidmultithreadinginputstreamandroid-3.0-honeycomb

提问by Indrek K?ue

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.

我在将输入流从 Web 转换为位图时遇到问题。仅当输入图像类型为 .BMP(位图)时才会出现问题。在这种情况下:bitmapFactory.decodeStream 返回 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);

采纳答案by Indrek K?ue

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

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

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

来源