android加载网络图片有一个bug,至少目前我在android1.5.jar中没有找到解决方案。
昨天关于jpg、gif和png的帖子已经提到,png是支持interlaced的。j2me、symbian、wm和iphone在网络加载interlaced格式的png时没有出现问题,但是android在网络加载interlaced格式的png时就会出现图像模糊不清的现象(根据interlaced原理,再经过观察,我估计是只扫描了奇数行而没有扫描偶数行,所以出现了图像模糊的状况)。
android网络加载图片的方式有两种:(1)用HttpURLConnection取得InputStream,再用InputStream流作为参数,利用android提供的api生成图片。(2)用HttpClick(apche的一个开源项目,被引用到android中了),取得图片流的byte[]信息,然后以byte[]作为参数生成图片。前文已有论述,遂不赘述。现只将实际图片贴上来以说明这个bug,希望做android底层开发的兄弟姐妹们能尽快帮忙修复这个bug。
1. interlaced格式图片
2. none interlaced格式图片
Bug-Report: Interlaced PNG bitmap acquired through network is blurred
I was trying to acquire interlaced PNG bitmap through network, there is no warn or errer in resulting output, but this interlaced PNG bitmap acquired from network is blurred, very blurred.
If the interlaced PNG is acquired from native(res/drawable), it is distinct;
If the PNG bitmap is not interlaced, load it from either network or native, it is distinct.
I think the reason is: android just scan odd-numbered or even-numbered rows of the interlaced PNG, but doesn't scan scan even-numbered or odd-numbered rows of the interlaced PNG
Main Codes:
public void surfaceCreated(SurfaceHolder holder) {
canvas = mHolder.lockCanvas();
Paint mPaint = new Paint();
try {
String strURL = "http://www.tiexin.com/images/map_blocks/1/4/3/3/00000037_00000031.png";
canvas.drawBitmap(getBitmap(strURL), 0, 0, mPaint);
} catch (IOException e) {
e.printStackTrace();
}
}
private Bitmap getBitmap(String strURL) throws IOException {
URL url = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(1000);
conn.setRequestMethod("GET");
conn.connect();
for (int i = 0; i < 5; i++) { // 连接5次
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is, null, null);
if (is != null) {
is.close();
is = null;
}
if (conn != null) {
conn.disconnect();
conn = null;
}
return bm;
}
}
return null;
}
Welcome to visit my blog to see the effect drawing
http://wayfarer.iteye.com/admin/blogs/442586