首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重设BufferedInputStream,保存FileInputStream

重设BufferedInputStream,保存FileInputStream
EN

Stack Overflow用户
提问于 2013-09-21 05:34:35
回答 1查看 1.7K关注 0票数 3

我正在分两步解码一个jpeg。

  1. 检查边界,必要时确定比例。
  2. 在屏幕范围内解码。

代码语言:javascript
复制
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的位置,但这似乎是一个相当尴尬的解决方案。没有办法正确地重置封装BufferedInputStreamFileInputStream吗?

EN

回答 1

Stack Overflow用户

发布于 2013-09-21 05:47:47

在使用InputStream.reset之前,您必须先调用InputStream.mark来标记以后要返回的位置。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18929300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档