将输入流转换为位图

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

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

平台: 安卓 (蜂窝)

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

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

答案 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);


答案 2

这是一行答案

val bitmap = BitmapFactory.decodeStream(inputStream)

返回Bitmap


推荐