title: large bitmap crushes (out of memory)
body: <p>I am following developer.android website <a href="http://developer.android.com/training/displaying-bitmaps/index.html" rel="nofollow">here</a> and I am trying load large image from url (i.e 1024 x 900) My code works fine in my Google Nexus but doesn't work on Nexus s. </p>
  
  <p>Here is my code</p>
  
  <pre><code>public static Bitmap getBitmapFromURL(String src) {
          Bitmap bmImg;
          URL myFileUrl = null;
  
          try {
              myFileUrl = new URL(src);
  
              HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
              conn.setDoInput(true);
              conn.connect();
              InputStream is = conn.getInputStream();
  
  
              final BitmapFactory.Options options = new BitmapFactory.Options();
  
              BufferedInputStream bis = new BufferedInputStream(is, 4*1024);
              ByteArrayBuffer baf = new ByteArrayBuffer(50);
              int current = 0;
              while ((current = bis.read()) != -1) {
                  baf.append((byte)current);
              }
              byte[] imageData = baf.toByteArray();
  
              BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
              options.inJustDecodeBounds = true;
              options.inSampleSize = calculateInSampleSize(options, 1024, 128);
              options.inJustDecodeBounds = false;
              Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
              return bitmap;
          } catch (Exception e) {
              // TODO Auto-generated catch block
              return null;
          }
      } 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public static int calculateInSampleSize(
              BitmapFactory.Options options, int reqWidth, int reqHeight) {
          // Raw height and width of image
          // 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 / 2;
              final int halfWidth = width / 2;
  
              // 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>AsyncTask class</p>
  
  <pre><code>class BitmapWorkerTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
          private final WeakReference&lt;ImageView&gt; imageViewReference;
          private String data = null;
  
          public BitmapWorkerTask(ImageView imageView) {
              // Use a WeakReference to ensure the ImageView can be garbage collected
              imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
          }
  
          // Decode image in background.
          @Override
          protected Bitmap doInBackground(String... params) {
              data = params[0];
              return Util.getBitmapFromURL(data);
          }
  
          // Once complete, see if ImageView is still around and set bitmap.
          @Override
          protected void onPostExecute(Bitmap bitmap) {
              if (imageViewReference != null &amp;&amp; bitmap != null) {
                  final ImageView imageView = imageViewReference.get();
                  if (imageView != null) {
                      imageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()/2));
                  }
              }
          }
      }
  </code></pre>
  
  <p>When I run on Nexus s I have out of memory exception. As following android developer website I didn't understand what I did wrong here and why my application crushes in old devices.
  Please help. </p>
  
  <p>Note: my image width should be match_parent always. That's why I put calculateInSampleSize(option,<strong>1024</strong>,128)</p>