问题描述
我试图通过位调整教程 - 唯一的区别是,我使用去codeStream而不是de codeResource。很奇怪,但德codeStream,无需任何操作,给了我一个位图OBJ,但是当我通过德codeSampledBitmapFromStream由于某种原因返回null。如何解决呢?
这里的code我用:
保护处理程序_onPromoBlocksLoad =新的处理程序(){
@覆盖
公共无效在DispatchMessage(信息MSG){
PromoBlocksContainer C =(PromoBlocksContainer)_promoBlocksFactory.getResponse();
HTT prequest请求=新HTT prequest(c.getPromoBlocks()得到(0).getSmallThumbnail());
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
为InputStream流;
ImageView的V =(ImageView的)findViewById(R.id.banner);
尝试 {
流= request.getStream();
//v.setImageBitmap(BitmapFactory.de$c$cStream(stream));做工精细
位图IMG =去codeSampledBitmapFromStream(流v.getWidth(),v.getHeight());
v.setImageBitmap(IMG);
}赶上(IOException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
}
};
公共静态INT calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1;
如果(高度> reqHeight ||宽度GT; reqWidth){
如果(宽>高度){
inSampleSize = Math.round((浮动)的高度/(浮点)reqHeight);
} 其他 {
inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
}
}
返回inSampleSize;
}
公共静态位图德codeSampledBitmapFromStream(InputStream的资源,INT reqWidth,诠释reqHeight){
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeStream(RES,空,期权);
options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);
options.inJustDe codeBounds = FALSE;
位图IMG = BitmapFactory.de codeStream(RES,空,期权); //给出空
返回IMG;
}
但问题是,一旦你使用一个InputStream从HttpURLConnection类,您不能倒带,并使用相同的InputStream再次 。因此,你必须创建一个新的InputStream为图像的实际采样。否则,我们不得不放弃了http请求。
request.abort();
I'm trying to adopt bitmap resizing tutorial - the only difference is that I use decodeStream instead of decodeResource. It's strange but decodeStream, without any manipulations, gives me a bitmap obj, but when I go through decodeSampledBitmapFromStream it returns null for some reason.How do I fix it ?
Here's the code I use:
protected Handler _onPromoBlocksLoad = new Handler() {
@Override
public void dispatchMessage(Message msg) {
PromoBlocksContainer c = (PromoBlocksContainer) _promoBlocksFactory.getResponse();
HttpRequest request = new HttpRequest(c.getPromoBlocks().get(0).getSmallThumbnail());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream stream;
ImageView v = (ImageView) findViewById(R.id.banner);
try {
stream = request.getStream();
//v.setImageBitmap(BitmapFactory.decodeStream(stream)); Works fine
Bitmap img = decodeSampledBitmapFromStream(stream, v.getWidth(), v.getHeight());
v.setImageBitmap(img);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromStream(InputStream res, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(res, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeStream(res, null, options); // Gives null
return img;
}
The problem was that once you've used an InputStream from a HttpUrlConnection, you can't rewind and use the same InputStream again. Therefore you have to create a new InputStream for the actual sampling of the image. Otherwise we have to abort the http request.
request.abort();
这篇关于德codeStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!