将输入流转换为位图

时间:2011-07-07 14:30:47

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

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

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

平台:Android(Honeycomb)

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

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

2 个答案:

答案 0 :(得分:55)

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

答案 1 :(得分:2)

这里是单行答案

val bitmap = BitmapFactory.decodeStream(inputStream)

返回Bitmap