我正在分两步解码一个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:马克已经失效了。
因此,我添加了instanceof部分来手动重置FileInputStream的位置,但这似乎是一个相当尴尬的解决方案。没有办法正确地重置封装BufferedInputStream的FileInputStream吗?
发布于 2013-09-21 05:47:47
在使用InputStream.reset之前,您必须先调用InputStream.mark来标记以后要返回的位置。
https://stackoverflow.com/questions/18929300
复制相似问题