我正在分两步解码jpeg。
public static Bitmap decodeSampledBitmapFromInputStream(InputStream data, int reqWidth, int reqHeight)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(data, null, options);
// Calculate inSampleSize
options.inSampleSize = Util.getExactSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
try {
// TODO: This works, but is there a better way?
if (data instanceof FileInputStream)
((FileInputStream)data).getChannel().position(0);
else
data.reset();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return BitmapFactory.decodeStream(data, null, options);
}
当底层流是FileInputStream时,它会在reset()
上崩溃:
java.io.IOException:Mark已失效。
所以我添加了instanceof
部分来手动重置FileInputStream
的位置,但这似乎是一个相当尴尬的解决方案。是否无法正确重置封装BufferedInputStream
的{{1}}?
答案 0 :(得分:3)
在使用InputStream.reset之前,您必须先调用InputStream.mark来标记您想要稍后返回的位置。