昨天在写一个带图片的列表页的时候遇到了一些问题
在自定义的ListAdapter中使用如下代码加载网络图片导致抛出NetworkOnMainThreadException异常
URL picUrl = new URL("http://www.abc.com/abc.jpg");Bitmap pngBM = BitmapFactory.decodeStream(picUrl.openStream());img.setImageBitmap(head); try catch后打印出异常信息,得到NetworkOnMainThreadException异常信息 百度后得知
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it‘s heavily discouraged. See the document Designing for Responsiveness.
解释一下,从Honeycomb SDK(3.0)开始,google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中,其实本来就不应该这样做,直接在UI线程进行网络操作,会阻塞UI、用户体验相当bad!即便google不禁止,一般情况下我们也不 会这么做吧~ 所以,也就是说,在Honeycomb SDK(3.0)以下的版本,你还可以继续在Main Thread里这样做,在3.0以上,就不行了
修改代码讲图片的网络加载请求放到一个单独的线程或者AsyncTask来完成,一切ok.
还有一个小问题就是Adapter中getView方法View的复用的问题导致图片看起来错位的感觉,是因为View的复用导致图片从新加载钱使用的是其他item的图片,使用LRuCache缓存起来每次加载的图片就可以解决!
public class ImageLoaders extends AsyncTask<String,Void,Bitmap> { String url = null; ImageView img = null; static LruCache<String , Bitmap> imageCache = new LruCache<String , Bitmap>( (int)Runtime.getRuntime().maxMemory()/1024); public ImageLoaders(String Url , ImageView imgView) { this.url = Url; this.img = imgView; //imageCache = new LruCache<String , Bitmap>( (int)Runtime.getRuntime().maxMemory()/1024); } /** * Override this method to perform a computation on a background thread. The * specified parameters are the parameters passed to {@link #execute} * by the caller of this task. * <p/> * This method can call {@link #publishProgress} to publish updates * on the UI thread. * * @param params The parameters of the task. * @return A result, defined by the subclass of this task. * @see #onPreExecute() * @see #onPostExecute * @see #publishProgress */ @Override protected Bitmap doInBackground(String... params) { try{ Log.i("img param", params[0]); Log.i("img cache" , ImageLoaders.imageCache.toString()); if (ImageLoaders.imageCache.get(params[0]) != null) return ImageLoaders.imageCache.get(params[0]); //Log.i("img cache" , ImageLoaders.imageCache.get(params[0])); URL picUrl = new URL(params[0]); InputStream is = picUrl.openStream(); Bitmap pngBM = BitmapFactory.decodeStream(is); Log.i("image data", is.toString()); ImageLoaders.imageCache.put(params[0], pngBM); return pngBM; }catch (Exception ex) { ex.printStackTrace(); } return null; } /** * <p>Runs on the UI thread after {@link #doInBackground}. The * specified result is the value returned by {@link #doInBackground}.</p> * <p/> * <p>This method won‘t be invoked if the task was cancelled.</p> * * @param head The result of the operation computed by {@link #doInBackground}. * @see #onPreExecute * @see #doInBackground * @see #onCancelled(Object) */ @Override protected void onPostExecute(Bitmap head) { //super.onPostExecute(is); img.setImageBitmap(head); } }
时间: 2024-10-09 23:44:01