Android Async LoadIcon OutOfMemoryError

时间:2013-12-06 03:24:32

标签: android memory-leaks

在某些特定设备上,应用程序的loadicon会抛出OutofMemoryError

private static class LoadIconTask extends AsyncTask<Object, Void, View> {
        @Override
        protected View doInBackground(Object... params) {
            try {
                final PackageInfoData app = (PackageInfoData) params[0];
                final PackageManager pkgMgr = (PackageManager) params[1];
                final View viewToUpdate = (View) params[2];
                if (!app.icon_loaded) {
                    app.cached_icon = pkgMgr.getApplicationIcon(app.appinfo);
                    app.icon_loaded = true;
                }
                // Return the view to update at "onPostExecute"
                // Note that we cannot be sure that this view still references
                // "app"
                return viewToUpdate;
            } catch (Exception e) {
                Log.e(TAG, "Error loading icon", e);
                return null;
            }
        }

在MainActivity上,

final ListAdapter adapter = new ArrayAdapter<PackageInfoData>(this,R.layout.main_list,  R.id.itemtext, apps2) {

 public View getView(final int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if ( holder != null) holder = new ViewHolder();
           ...
  holder.icon.setImageDrawable(holder.app.cached_icon); 
  if (!holder.app.icon_loaded && info != null) {
     //this icon has not been loaded yet - load it on a
     // separated thread
     try {
    new LoadIconTask().execute(holder.app, getPackageManager(),convertView);
     } catch (RejectedExecutionException r) { }
    ...
   }
}

PackageInfoData类

public static final class PackageInfoData {
        ApplicationInfo appinfo;
        /** cached application icon */
        Drawable cached_icon;
        /** indicates if the icon has been loaded already */
        boolean icon_loaded;

}

我试图在我的设备上重现这一点,但是由于应用程序数量有限,这种情况不会发生。有什么帮助吗?

0 个答案:

没有答案