title: Android: "trying to use a recycled bitmap" error with temporary Bitmaps
body: <p>My app can load quite large images. In an effort to be memory-conservative, I'm attempting to use a temporary bitmap to load and another for the final image after transformation:</p>
  
  <pre><code>.....
  finalBitmap.recycle();
  finalBitmap = null;
  Bitmap tempBitmap  = BitmapFactory.decodeStream(fin, ...);
  finalBitmap = Bitmap.createBitmap(tempBitmap, ....);
  imgview.setImageBitmap(finalBitmap);
  .....
  </code></pre>
  
  <p>Now, at this point we're done with tempBitmap, which was only needed to transport the decoded Bitmap to the transformation step in createBitmap. So:</p>
  
  <pre><code>.....
  tempBitmap.recycle();
  tempBitmap = null;
  .....
  </code></pre>
  
  <p>And... it crashes with a "trying to use a recycled bitmap" error specifically because of the recycling of tempBitmap. tempBitmap wasn't displayed and is only used <em>right there</em>.</p>
  
  <p><strong>What's going wrong here?</strong> Should I just use "finalBitmap" throughout and rely on createBitmap to manage it (<strong><em>finalBitmap</strong> = Bitmap.createBitmap(<strong>finalBitmap</strong> , ....)</em>)? I fail to see what ongoing dependency on tempBitmap there would be that would cause such a failure.</p>
  
  <p><strong>Edit:</strong> Yes, the null assignment seems to result in the appropriate, <em>eventual</em> garbage collection, but I'm mystified as to why recycle() on a temp Bitmap is so problematic in this case. I get the impression that createBitmap() is holding a reference to it but why, and for how long?</p>