gpt4 book ai didi

java - 将输入流转换为位图

转载 作者:行者123 更新时间:2023-11-29 09:10:15 25 4
gpt4 key购买 nike

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

关于如何解决这个问题或我应该在哪里继续调试的任何提示?

平台:安卓( hive )

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

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

最佳答案

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

关于java - 将输入流转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12822917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com