Android 使用BitmapFactory.Options会导致BitmapFactory中的更改

Android 使用BitmapFactory.Options会导致BitmapFactory中的更改,android,bitmap,bitmapfactory,Android,Bitmap,Bitmapfactory,我想要实现的是能够从inputstream计算位图的高度和宽度,而无需实际更改BitmapFactory.Options 这就是我所做的: private Boolean testSize(InputStream inputStream){ BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options(); Bitmp_Options.inJustDecodeBounds = true; BitmapFac

我想要实现的是能够从inputstream计算位图的高度和宽度,而无需实际更改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。选项吗?还是另一种可能的解决方案

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

这是我的原始代码:

Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);
应用Deev和Nobu Game的建议:(无变化)


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

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

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

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

当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());
}

}一个简单的类,当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());
}

}

为什么不创建另一个BitmapFactory.Options对象?谢谢您的想法。我仍然有这个问题没有图像显示在另一个方法上我有上面更新的方法。我开始认为这是一个错误。为什么不创建另一个BitmapFactory.Options对象?谢谢你的想法。我仍然有这个问题,没有图像显示在另一个方法上,我已经更新了上面的内容。我开始认为这是一个错误。我没有试图重用它。没有错误消息。我没有重用option对象。对于最后一个,我创建了BitmapFactory.Options对象,但仍然不好。当我尝试执行BitmapFactory.Options=new BitmapFactory.Options()时,不会显示任何图像;options.inJustDecodeBounds=false;位图originalBitmap=BitmapFactory.decodeStream(InpStream,null,options);我不想重复使用它。没有错误消息。我没有重用option对象。对于最后一个,我创建了BitmapFactory.Options对象,但仍然不好。当我尝试执行BitmapFactory.Options=new BitmapFactory.Options()时,不会显示任何图像;options.inJustDecodeBounds=false;位图originalBitmap=BitmapFactory.decodeStream(InpStream,null,options);