Rip*_*yom 5 android gallery bitmapfactory
我正在开发一个Android应用程序,它的视图包含多个Gallerys.Gallerys(Bitmaps)的内容是互联网上的红色.
对于第一个库,一切正常,但在尝试下载第二个Gallery的第一个图像时,BitmapFactory.decodeStream(InputStream)返回null,而流是非null.
public void loadBitmap() throws IOException {
for (int i = 0; i < images.size(); ++i) {
URL ulrn = new URL(images.get(i).getThumbUrl());
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
images.get(i).setImage(BitmapFactory.decodeStream(is));
Log.i("MY_TAG", "Height: " + images.get(i).getImage().getHeight());
}
}
Run Code Online (Sandbox Code Playgroud)
所述getThumbUrl()返回图像的URL(例如http://mydomain.com/image.jpg)和它抛出一个NullPointerException在线路Log.i("MY_TAG", "Height: ... )
(images为ArrayList我的类,其保持URL和位图也包含对象).
谢谢你的建议!
我遇到过这个.尝试将BufferedHttpEntity与您的输入流一起使用.我发现这可以防止99.9%的问题从decodeStream获取静默空值.
也许不是很重要,但我可靠地使用org.apache.http.client.HttpClient而不是HttpURLConnection,如:
public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
{
HttpResponse response=null;
Bitmap b=null;
InputStream instream=null;
BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
decodeOptions.inPreferredConfig = bitmapCOnfig;
try
{
HttpGet request = new HttpGet(url.toURI());
response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200)
{
MyLogger.w("Bad response on " + url.toString());
MyLogger.w ("http response: " + response.getStatusLine().toString());
return null;
}
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
instream = bufHttpEntity.getContent();
return BitmapFactory.decodeStream(instream, null, decodeOptions);
}
catch (Exception ex)
{
MyLogger.e("error decoding bitmap from:" + url, ex);
if (response != null)
{
MyLogger.e("http status: " + response.getStatusLine().getStatusCode());
}
return null;
}
finally
{
if (instream != null)
{
try {
instream.close();
} catch (IOException e) {
MyLogger.e("error closing stream", e);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9540 次 |
| 最近记录: |