【问题标题】:Using BitmapFactory.Options causes changes in BitmapFactory使用 BitmapFactory.Options 会导致 BitmapFactory 发生变化
【发布时间】:2012-06-25 18:38:37
【问题描述】:

我想要实现的是能够从输入流计算位图的高度和宽度,而无需实际更改 BitmapFactory.Options

这就是我所做的:

private Boolean testSize(InputStream inputStream){
    BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
    Bitmp_Options.inJustDecodeBounds = true;
    BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
    int currentImageHeight = Bitmp_Options.outHeight;
    int currentImageWidth = Bitmp_Options.outWidth;
    if(currentImageHeight > 200 || currentImageWidth > 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }
    return false;
}

现在这里的主要问题是它将 BitmapFactory.Options 更改为无法正确解码流的状态。

我的问题是有另一种重置 BitmapFactory.Options 的方法吗?还是其他可能的解决方案?

另一种方法:(注意*应用top方法时originalBitmap为null)

这是我的原始代码:

Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);

应用 Deev 和 Nobu Game 的建议:(不变)

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream,null,options);

【问题讨论】:

  • 为什么不创建另一个 BitmapFactory.Options 对象?
  • 感谢您的想法。我仍然有这个问题没有图像显示在另一种方法上我有上面更新的内容。
  • 我开始认为这是一个错误。

标签: android bitmap bitmapfactory


【解决方案1】:

您正尝试从同一流中读取两次。流不能用作字节数组。一旦您从中读取数据,除非您重置流位置,否则您将无法再次读取它。您可以在第一次调用 decodeStream() 后尝试调用 InputStream.reset(),但并非所有 InputStream 都支持此方法。

【讨论】:

    【解决方案2】:

    如果您尝试重用您的 Options 对象(顺便说一下,您的代码示例中并非如此),那么您将如何尝试重用它?错误信息是什么,出了什么问题? 您是否尝试重用选项对象来实际解码位图?然后只需将 inJustDecodeBounds 设置为 false。

    【讨论】:

    • 我不想重复使用它。没有错误信息。我没有重用选项对象。最后一个我创建了 BitmapFactory.Options 对象,但还是不行。当我尝试执行 BitmapFactory.Options options = new BitmapFactory.Options(); 时没有显示图像options.inJustDecodeBounds = false;位图 originalBitmap = BitmapFactory.decodeStream(InpStream,null,options);
    【解决方案3】:

    当 inputStream.Mark 和 inputStream.Reset 不起作用时复制 InputStream 的简单类。

    打电话:

    CopyInputStream copyStream = new CopyInputStream(zip);
    InputStream inputStream = copyStream.getIS();
    

    我希望这对某人有所帮助。这是代码。

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    public class CopyInputStream {
    
    private InputStream inputStream;
    private ByteArrayOutputStream byteOutPutStream;
    
    /*
     * Copies the InputStream to be reused
     */
    public CopyInputStream(InputStream is){
        this.inputStream = is;
        try{
            int chunk = 0;
            byte[] data = new byte[256];
    
            while(-1 != (chunk = inputStream.read(data)))
            {
                byteOutPutStream.write(data, 0, chunk);
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
    /*
     * Calls the finished inputStream
     */
    public InputStream getIS(){
        return (InputStream)new ByteArrayInputStream(byteOutPutStream.toByteArray());
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2010-09-30
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 2021-06-15
      相关资源
      最近更新 更多