【问题标题】:Converting input stream into bitmap将输入流转换为位图
【发布时间】:2011-09-30 12:49:09
【问题描述】:

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

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

平台:Android(蜂窝)

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

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

【问题讨论】:

  • 是否有任何日志错误无法提供帮助?

标签: java android multithreading inputstream android-3.0-honeycomb


【解决方案1】:

感谢@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);

Source

【讨论】:

  • 我使用 WCF 服务,当我发送图像的字节 [] 并使用您的方法转换回位图图像时,但 BitmapFactory.decodeStream(instream) 始终为空!!
【解决方案2】:

这里是单行答案

val bitmap = BitmapFactory.decodeStream(inputStream)

返回Bitmap

【讨论】:

  • 例如,随意使用它来获取本地图像
猜你喜欢
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多