title: Android Universal Image Loader and GridView "Out of memory" on second launch
body: <p>I have checked all the related posts so far, but did not managed to fix this "Out of memory on a 1843216-byte allocation" error my GridView activity generates. </p>
  
  <p>Basically this activity displays the images from the phone's gallery in a GridView (as a multiple image picker), at first it loads everything just fine, at the second try I get the memory error and the GridView skips loading some of the image thumbnails.</p>
  
  <p>I have disabled the memory caching by: ".cacheInMemory(false)", and using only disk caching with fixed ImageView width and height, but still got the memory error. </p>
  
  <p>Could someone help me out? Thanks!</p>
  
  <p>The CustomGalleryActivity, showing all pictures on the phone:</p>
  
  <pre><code>public class CustomGalleryActivity extends Activity {
  
      GridView gridGallery;
      Handler handler;
      GalleryAdapter adapter;
  
      ImageView imgNoMedia;
      Button btnGalleryOk;
  
      String action;
      private ImageLoader imageLoader;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.gallery);
  
          action = getIntent().getAction();
          if (action == null) {
              finish();
          }
  
          initImageLoader();
          init();
      }
  
      private void initImageLoader() {
          try {
              String CACHE_DIR = Environment.getExternalStorageDirectory()
                      .getAbsolutePath() + "/.temp_tmp";
              new File(CACHE_DIR).mkdirs();
  
              File cacheDir = StorageUtils.getOwnCacheDirectory(getBaseContext(),
                      CACHE_DIR);
  
              DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()                      
                      .imageScaleType(ImageScaleType.EXACTLY)
                      .bitmapConfig(Bitmap.Config.RGB_565)
                      .cacheInMemory(false)
                      .cacheOnDisk(true)
                      .considerExifParams(true)                   
                      .build();
  
              ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(getBaseContext())           
                      .threadPriority(Thread.NORM_PRIORITY - 2)                       
                      .denyCacheImageMultipleSizesInMemory()
                      .defaultDisplayImageOptions(defaultOptions)
                      .discCache(new UnlimitedDiscCache(cacheDir))                    
                      .tasksProcessingOrder(QueueProcessingType.LIFO);                
                      //.memoryCache(new WeakMemoryCache());                  
  
              L.writeLogs(false); 
              ImageLoaderConfiguration config = builder.build();
              imageLoader = ImageLoader.getInstance();            
              imageLoader.destroy();
              imageLoader.init(config);
  
  
          } catch (Exception e) {
  
          }
      }
  
      private void init() {
  
          handler = new Handler();
          gridGallery = (GridView) findViewById(R.id.gridGallery);
          gridGallery.setFastScrollEnabled(true);
          adapter = new GalleryAdapter(getApplicationContext(), imageLoader);
          PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader,
                  true, true);
          gridGallery.setOnScrollListener(listener);
  
          if (action.equalsIgnoreCase(Action.ACTION_MULTIPLE_PICK)) {
  
              findViewById(R.id.llBottomContainer).setVisibility(View.VISIBLE);
              gridGallery.setOnItemClickListener(mItemMulClickListener);
              adapter.setMultiplePick(true);
  
          } else if (action.equalsIgnoreCase(Action.ACTION_PICK)) {
  
              findViewById(R.id.llBottomContainer).setVisibility(View.GONE);
              gridGallery.setOnItemClickListener(mItemSingleClickListener);
              adapter.setMultiplePick(false);
  
          }
  
          gridGallery.setAdapter(adapter);
          imgNoMedia = (ImageView) findViewById(R.id.imgNoMedia);
  
          btnGalleryOk = (Button) findViewById(R.id.btnGalleryOk);
          btnGalleryOk.setOnClickListener(mOkClickListener);
  
          new Thread() {
  
              @Override
              public void run() {
                  Looper.prepare();
                  handler.post(new Runnable() {
  
                      @Override
                      public void run() {
                          adapter.addAll(getGalleryPhotos());
                          checkImageStatus();
                      }
                  });
                  Looper.loop();
              };
  
          }.start();
  
      }
  
      private void checkImageStatus() {
          if (adapter.isEmpty()) {
              imgNoMedia.setVisibility(View.VISIBLE);
          } else {
              imgNoMedia.setVisibility(View.GONE);
          }
      }
  
      View.OnClickListener mOkClickListener = new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              ArrayList&lt;CustomGallery&gt; selected = adapter.getSelected();
  
              String[] allPath = new String[selected.size()];
              for (int i = 0; i &lt; allPath.length; i++) {
                  allPath[i] = selected.get(i).sdcardPath;
              }
  
              Intent data = new Intent().putExtra("all_path", allPath);
              setResult(RESULT_OK, data);
  
              finish();
  
          }
      };
      AdapterView.OnItemClickListener mItemMulClickListener = new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; l, View v, int position, long id) {
              //adapter.changeSelection(v, position);
              if (adapter.getSelected().size() &gt;= 15) {
                  Toast.makeText(getApplicationContext(), "Max. 15 de poze pot fi selectate o data!", Toast.LENGTH_LONG).show();
              } else {
                  adapter.changeSelection(v, position);
              }
  
          }
      };
  
      AdapterView.OnItemClickListener mItemSingleClickListener = new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; l, View v, int position, long id) {
              CustomGallery item = adapter.getItem(position);
              Intent data = new Intent().putExtra("single_path", item.sdcardPath);
              setResult(RESULT_OK, data);
              finish();
          }
      };
  
      private ArrayList&lt;CustomGallery&gt; getGalleryPhotos() {
          ArrayList&lt;CustomGallery&gt; galleryList = new ArrayList&lt;CustomGallery&gt;();
  
          try {
              final String[] columns = { MediaStore.Images.Media.DATA,
                      MediaStore.Images.Media._ID };
              final String orderBy = MediaStore.Images.Media._ID;
  
              @SuppressWarnings("deprecation")
              Cursor imagecursor = managedQuery(
                      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                      null, null, orderBy);
  
              if (imagecursor != null &amp;&amp; imagecursor.getCount() &gt; 0) {
  
                  while (imagecursor.moveToNext()) {
                      CustomGallery item = new CustomGallery();
  
                      int dataColumnIndex = imagecursor
                              .getColumnIndex(MediaStore.Images.Media.DATA);
  
                      item.sdcardPath = imagecursor.getString(dataColumnIndex);
  
                      galleryList.add(item);
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          // show newest photo at beginning of the list
          Collections.reverse(galleryList);
          return galleryList;
      }
  
  }
  </code></pre>
  
  <p>And its CustomAdapter:</p>
  
  <pre><code>public class GalleryAdapter extends BaseAdapter {
  
      private Context mContext;
      private LayoutInflater infalter;
      private ArrayList&lt;CustomGallery&gt; data = new ArrayList&lt;CustomGallery&gt;();
      ImageLoader imageLoader;
  
      private boolean isActionMultiplePick;
  
      public GalleryAdapter(Context c, ImageLoader imageLoader) {
          infalter = (LayoutInflater) c
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          mContext = c;
          this.imageLoader = imageLoader;
          // clearCache();
      }
  
      @Override
      public int getCount() {
          return data.size();
      }
  
      @Override
      public CustomGallery getItem(int position) {
          return data.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      public void setMultiplePick(boolean isMultiplePick) {
          this.isActionMultiplePick = isMultiplePick;
      }
  
      public void selectAll(boolean selection) {
          for (int i = 0; i &lt; data.size(); i++) {
              data.get(i).isSeleted = selection;
  
          }
          notifyDataSetChanged();
      }
  
      public boolean isAllSelected() {
          boolean isAllSelected = true;
  
          for (int i = 0; i &lt; data.size(); i++) {
              if (!data.get(i).isSeleted) {
                  isAllSelected = false;
                  break;
              }
          }
  
          return isAllSelected;
      }
  
      public boolean isAnySelected() {
          boolean isAnySelected = false;
  
          for (int i = 0; i &lt; data.size(); i++) {
              if (data.get(i).isSeleted) {
                  isAnySelected = true;
                  break;
              }
          }
  
          return isAnySelected;
      }
  
      public ArrayList&lt;CustomGallery&gt; getSelected() {
          ArrayList&lt;CustomGallery&gt; dataT = new ArrayList&lt;CustomGallery&gt;();
  
          for (int i = 0; i &lt; data.size(); i++) {
              if (data.get(i).isSeleted) {
                  dataT.add(data.get(i));
              }
          }
  
          return dataT;
      }
  
      public void addAll(ArrayList&lt;CustomGallery&gt; files) {
  
          try {
              this.data.clear();
              this.data.addAll(files);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          notifyDataSetChanged();
      }
  
      public void changeSelection(View v, int position) {
  
          if (data.get(position).isSeleted) {
              data.get(position).isSeleted = false;
          } else {
              data.get(position).isSeleted = true;
          }
  
          ((ViewHolder) v.getTag()).imgQueueMultiSelected.setSelected(data
                  .get(position).isSeleted);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          final ViewHolder holder;
          if (convertView == null) {
  
              convertView = infalter.inflate(R.layout.gallery_item, null);
              holder = new ViewHolder();
              holder.imgQueue = (ImageView) convertView
                      .findViewById(R.id.imgQueue);
  
              holder.imgQueueMultiSelected = (ImageView) convertView
                      .findViewById(R.id.imgQueueMultiSelected);
  
              if (isActionMultiplePick) {
                  holder.imgQueueMultiSelected.setVisibility(View.VISIBLE);
              } else {
                  holder.imgQueueMultiSelected.setVisibility(View.GONE);
              }
  
              convertView.setTag(holder);
  
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
          holder.imgQueue.setTag(position);
  
          try {
  
              imageLoader.displayImage("file://" + data.get(position).sdcardPath,
                      holder.imgQueue, new SimpleImageLoadingListener() {
                          @Override
                          public void onLoadingStarted(String imageUri, View view) {
                              holder.imgQueue.setImageResource(R.drawable.no_media);
                              super.onLoadingStarted(imageUri, view);
                          }
                      });
  
              if (isActionMultiplePick) {
  
                  holder.imgQueueMultiSelected
                          .setSelected(data.get(position).isSeleted);
  
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return convertView;
      }
  
      public class ViewHolder {
          ImageView imgQueue;
          ImageView imgQueueMultiSelected;
      }
  
      public void clearCache() {
          imageLoader.clearDiscCache();
          imageLoader.clearMemoryCache();
      }
  
      public void clear() {
          data.clear();
          notifyDataSetChanged();
      }
  }
  </code></pre>