1
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;
4

1 回答 1

0

您始终可以从 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()如果您的代码在流上调用,请小心。如果发生这种情况,只需ByteArrayInputStream使用ByteArrayOutputStream out.toByteArray().

于 2013-09-12T15:36:28.010 回答