gpt4 book ai didi

java - BitmapFactory.decodeStream(stream, null, options) 时重用InputStream

转载 作者:行者123 更新时间:2023-12-01 04:22:54 25 4
gpt4 key购买 nike

InputStream stream = new URL(key).openStream();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(stream, null, options);

options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;

/* i need reuse stream here, for decode stream again. */

Bitmap bmp = BitmapFactory.decodeStream(stream, null, options);

stream.close();

return bmp;

最佳答案

您始终可以从 URL 重新打开流

InputStream stream = new URL(key).openStream();

或者使用像 Guava (ByteStreams) 这样的库来完全读取 InputStream 并将结果字节分配到 ByteArrayInputStream

InputStream stream = new URL(key).openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteStreams.copy(stream, out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

// use it once
in.reset();
// use it again

如果您的代码在流上调用 mark(),请务必小心。如果发生这种情况,只需使用 ByteArrayOutputStream out.toByteArray() 中的字节创建一个新的 ByteArrayInputStream

关于java - BitmapFactory.decodeStream(stream, null, options) 时重用InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18768422/

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