title: Android OutOfMemoryError when loading GridView bitmaps
body: <p>I am trying to load some very small images (average size is 90kb) into a gridview in Android.  Whenever I load more than 9 images then I am getting memory issues.  I have tried scaling the images to a smaller size and although this works to a certain extent it is not really a true solution as the picture quality is awful.</p>
  
  <p>The code is below</p>
  
  <pre><code>private Context mContext;
  private ArrayList&lt;Bitmap&gt; photos = new ArrayList&lt;Bitmap&gt;();
  public Bitmap [] mThumbIds;
  
  public ImageAdapter(Context c) {
      mContext = c;
  }
  
  
  public Object getItem(int position) {
      return null;
  }
  
  public long getItemId(int position) {
      return 0;
  }
  
  public Bitmap scaleBitmap(String imagePath) {
      Bitmap resizedBitmap = null;
      try {
          int inWidth = 0;
          int inHeight = 0;
  
          InputStream in;
  
          in = new FileInputStream(imagePath);
  
          // decode image size (decode metadata only, not the whole image)
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(in, null, options);
          in.close();
          in = null;
  
          // save width and height
          inWidth = options.outWidth;
          inHeight = options.outHeight;
  
          // decode full image pre-resized
          in = new FileInputStream(imagePath);
          options = new BitmapFactory.Options();
          // calc rought re-size (this is no exact resize)
          options.inSampleSize = Math.max(inWidth/300, inHeight/300);
          // decode full image
          Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);
  
          // calc exact destination size
          Matrix m = new Matrix();
          RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
          RectF outRect = new RectF(0, 0, 300, 300);
          m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
          float[] values = new float[9];
          m.getValues(values);
  
          // resize bitmap
          resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return resizedBitmap;
  }
  
  
  public void populateGrid() {
      File sdDir = new File("mnt/sdcard/Pictures");
      File[] sdDirFiles = sdDir.listFiles();
      for(File singleFile : sdDirFiles) {
          String filePath = singleFile.getAbsolutePath();
          Bitmap bmp = scaleBitmap(filePath);
          photos.add(bmp);
      }
      mThumbIds = photos.toArray(new Bitmap[(photos.size())]);
  }
  
  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {  // if it's not recycled, initialize some attributes
          imageView = new ImageView(mContext);
          imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          imageView.setPadding(8, 8, 8, 8);
      } else {
          imageView = (ImageView) convertView;
      }
      imageView.setImageBitmap(mThumbIds[position]);
      return imageView;
  }
  
  @Override
  public int getCount() {
      return mThumbIds.length;
  }
  }
  </code></pre>