title: How to load large images in Android and avoiding the out of memory error?
body: <p>I'm working on an app that uses large images (1390 × 870 : 150kb - 50kb). I'm adding images as I tap a trigger/ImageView.</p>
  
  <p>At a certain point I'm getting an out of memory error:</p>
  
  <pre><code>java.lang.OutOfMemoryError
  E/AndroidRuntime(23369): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
  E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
  E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
  </code></pre>
  
  <p>To resize the image I'm doing this:</p>
  
  <pre><code>Bitmap productIndex = null;
  final String imageLoc = IMAGE_LOCATION;
  InputStream imageStream;
  try {
       imageStream = new FileInputStream(imageLoc);
       productIndex = decodeSampledBitmapFromResource(getResources(), imageLoc, 400, 400);
  
       productIV.setImageBitmap(productIndex);
       } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
       }
  }
  
  
  public static Bitmap decodeSampledBitmapFromResource(Resources res, String resId, int reqWidth, int reqHeight) {
  
  // First decode with inJustDecodeBounds=true to check dimensions
  final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(resId, options);
  
  // Calculate inSampleSize
  options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;
  return BitmapFactory.decodeFile(resId, options);
  }
  
  public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  int inSampleSize = 1;
  
  if (height &gt; reqHeight || width &gt; reqWidth) {
  
      final int halfHeight = height / 3;
      final int halfWidth = width / 3;
  
      // Calculate the largest inSampleSize value that is a power of 2 and keeps both
      // height and width larger than the requested height and width.
      while ((halfHeight / inSampleSize) &gt; reqHeight
              &amp;&amp; (halfWidth / inSampleSize) &gt; reqWidth) {
          inSampleSize *= 2;
      }
  }
  
  return inSampleSize;
  }
  </code></pre>
  
  <p>I got this way of resizing to save space from the Android Docs:
  <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html">Loading Large Bitmaps Efficiently</a></p>
  
  <p>According to the log this like is the culprit in the <code>decodeSampledBitmapFromResource</code> method :</p>
  
  <pre><code>return BitmapFactory.decodeFile(resId, options);
  </code></pre>
  
  <p>----- edit -----
  Here is how I'm adding each item to the FrameLayout.</p>
  
  <pre><code>for(int ps=0;ps&lt;productSplit.size();ps++){
      //split each product by the equals sign
      List&lt;String&gt; productItem = Arrays.asList(productSplit.get(ps).split("="));
  
      String tempCarID = productItem.get(0);
      tempCarID = tempCarID.replace(" ", "");
      if(String.valueOf(carID).equals(tempCarID)){
  
          ImageView productIV = new ImageView(Configurator.this);
          LayoutParams productParams = new LayoutParams(
                  LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
          productIV.setId(Integer.parseInt(partIdsList.get(x)));
          productIV.setLayoutParams(productParams);
  
          final String imageLoc = productItem.get(2);
  
          InputStream imageStream;
          try {
              imageStream = new FileInputStream(imageLoc);
              productIndex = decodeSampledBitmapFromResource(getResources(), imageLoc, 400, 400);
              productIV.setImageBitmap(productIndex);
          } catch (FileNotFoundException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          productLayers.addView(productIV);
  
      }
  }
  </code></pre>