我想要实现的是能够从输入流计算位图的高度和宽度,而不需要实际更改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更改为不能正确decodeStream的状态。
我的问题是,有没有其他重置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);发布于 2012-06-27 08:10:13
您正尝试从同一个流中读取两次。流不能作为字节数组工作。一旦从其中读取了数据,就不能再次读取它,除非重置流位置。您可以在第一次调用InputStream.reset()之后尝试调用decodeStream(),但并不是所有的InputStreams都支持此方法。
发布于 2012-06-26 03:21:21
如果您正在尝试重用Options对象(顺便说一下,这不是您的代码示例中的情况),那么您将如何尝试重用它?错误消息是什么,出错的原因是什么?您是否正在尝试重用Options对象来实际解码Bitmap?然后将inJustDecodeBounds设置为false。
发布于 2012-06-27 22:06:55
一个简单的类,用于在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());
}}
https://stackoverflow.com/questions/11195287
复制相似问题