BitmapFactory.de codeStream无一例外返回null [英] BitmapFactory.decodeStream returns null without exception
问题描述
我尝试加载从服务器,并感谢远程图像很多code例子在计算器我有一个解决方案,进2出3张图片这工作。我真的不知道问题出在第三个画面是什么,有时甚至让在调试器中图片加载code运行时。另外,如果我第一次加载图片的问题有时不加载其他两个照片。
I try to load a remote image from a server and thanks to a lot of code examples on stackoverflow I have a solution which works in 2 out of 3 images. I don't really know what the problem is with the third picture and sometimes when letting the code run in the debugger the picture is loading. Also if I load the problem picture first the other two pictures are sometimes not loaded.
下面是code:
public static Drawable getPictureFromURL(Context ctx, String url, final int REQUIRED_SIZE) throws NullPointerException {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
int scale = 1;
if (o.outWidth > REQUIRED_SIZE) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(REQUIRED_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
Log.i(Prototype.TAG, "scale: "+scale);
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp;
try {
bmp = BitmapFactory.decodeStream((InputStream) Tools.fetch(url), null, o2);
if(bmp!=null)
return new BitmapDrawable(ctx.getResources(), bmp);
else
return null;
} catch (Exception e) {
Log.e(Prototype.TAG, "Exception while decoding stream", e);
return null;
}
}
在调试过程中我发现,o.outWidth为-1表示一个错误,但没有异常被抛出,所以我真的不能告诉哪里出了问题。 InputStream的总是返回一个有效的值,我知道图片存在于服务器上。
During debugging I found out that o.outWidth is -1 which indicates an error, but no Exception is thrown, so I can't really tell what went wrong. The InputStream always returned a valid value, and I know that the picture exists on the server.
最良好的祝愿, 丹尼尔·
Best wishes, Daniel
推荐答案
我找到了答案<一href="http://brainflush.word$p$pss.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/#comment-747">here和更新的获取方法:
I found the answer here and updated the fetch method to:
private static InputStream fetch(String address) throws MalformedURLException,IOException {
HttpGet httpRequest = new HttpGet(URI.create(address) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
return instream;
}
这篇关于BitmapFactory.de codeStream无一例外返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!