重用的InputStream时BitmapFactory.de codeStream(流空,期权) [英] Reuse InputStream when BitmapFactory.decodeStream(stream, null, options)

查看:159
本文介绍了重用的InputStream时BitmapFactory.de codeStream(流空,期权)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的InputStream流=新的URL(键).openStream();BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;BitmapFactory.de codeStream(流空,期权);options.inSampleSize = calculateInSampleSize(选项,reqWidth,
            reqHeight);
options.inJustDe codeBounds = FALSE;/ *我需要在这里再次重用流,德code流。 * /BMP位图= BitmapFactory.de codeStream(流空,期权);stream.close();返回BMP;


解决方案

您可以随时刚刚从URL重新打开流

 的InputStream流=新的URL(键).openStream();

或者我们像番石榴(字节流)充分阅读的InputStream 并将所得的字节分配到一个库 ByteArrayInputStream的

 的InputStream流=新的URL(键).openStream();
ByteArrayOutputStream出=新ByteArrayOutputStream();
ByteStreams.copy(流出来);
ByteArrayInputStream的在=新ByteArrayInputStream进行(out.toByteArray());//使用一次
in.reset();
//再次使用它

小心,如果你的code调用标记()在流。如果出现这种情况,而不仅仅是创建一个新的 ByteArrayInputStream的 ByteArrayOutputStream 出的字节数。 toByteArray()

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;

解决方案

You can always just reopen the stream from the URL

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

Or us a library like Guava (ByteStreams) to fully read the InputStream and assign the resulting bytes into a 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

Careful if your code calls mark() on the stream. If that happens, instead just create a new ByteArrayInputStream with the bytes from the ByteArrayOutputStream out.toByteArray().

这篇关于重用的InputStream时BitmapFactory.de codeStream(流空,期权)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆