title: java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap
body: <p>I send message to this handler(here I ommitted some cases) to do some operation on a bitmap and then set the bitmap on the UI. Because the operation on the bitmap is time consuming, I put it in a thread. When I finishing doing this, I set the UI in the UI thread with the method <strong><em>runOnUiThread</em></strong>. <br />
  Normally, all this runs well, but in some cases I got the following exception: <strong><em>java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap</em></strong>.<br />
  It occurs very rare, but it did happened several times. </p>
  
  <pre><code>private Handler mHandler = new Handler()
  {
      @Override
      public void handleMessage(Message msg)
      {
          int what = msg.what;
          switch (what)
          {
  
              case VideoProtocal.COMPRESS_SUCCESS:
                  state = State.COMPRESSED;
                  recordBack.setVisibility(View.VISIBLE);
                  recordSend.setVisibility(View.VISIBLE);
                  playButton.setVisibility(View.VISIBLE);
                  new Thread(){
                      @Override
                      public void run()
                      {
                          rotationBitmap();
                          runOnUiThread(new Runnable() {
                              public void run() {
                                  if( null != bitmap &amp;&amp; !bitmap.isRecycled() )
                                  {
                                      Drawable drawable = new BitmapDrawable(bitmap);
                                      fristFrame.setBackgroundDrawable(drawable);
                                  }
                              }
                          });
                      }
                  }.start();
                  mFrameCount = 0;
                  getVideoSize();
                  videoTime = getVideoFileTime(mp4_path) / 1000;
                  timeView.setText(getString(R.string.video_count, videoTime));
                  deleteTempFile();
                  LogX.trace(TAG, "COMPRESS_SUCCESS");
                  dismissProgress();
                  break;
              default:
                  break;
          }
      }
  
  };
  </code></pre>
  
  <p>The method rotationBitmap() and some method it calls is shown as bellow:</p>
  
  <pre><code>private void rotationBitmap()
  {
      int width = (int) (PIC_WIDTH * FusionField.currentDensity);
      int height = (int) (PIC_HIGHT * FusionField.currentDensity);
      Bitmap bmTemp = bitmap;
      bitmap = CommonUtil.resizeImage(bitmap, width, height);
  
      if (bmTemp != null &amp;&amp; !bmTemp.isRecycled() &amp;&amp; bmTemp != bitmap)
      {
          bmTemp.recycle();
          bmTemp = null;
      }
  
      rotate(bitmap, rotationAngle());
  
      CommonUtil.checkMysoftStage(FusionCode.videoThumbnail);
      String videoName = CommonUtil.getUniqueName(".jpg", FusionCode.videoName);
      jpg_path = FusionCode.videoThumbnail + "/" + videoName;
      try
      {
          FileOutputStream fos = new FileOutputStream(jpg_path);
          if (bitmap != null)
          {
              bitmap.compress(CompressFormat.JPEG, 50, fos);
          }
          fos.close();
      }
      catch (FileNotFoundException e)
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      catch (IOException e)
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  
  private Bitmap rotate(Bitmap b, int degrees)
  {
      if (degrees != 0 &amp;&amp; b != null)
      {
          Matrix m = new Matrix();
          m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
          try
          {
              bitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
          }
          catch (OutOfMemoryError ex)
          {
              // TODO Auto-generated catch block
              ex.printStackTrace();
          }
      }
      return b;
  }
  </code></pre>
  
  <p>From the code, the problem must ocuured in this statement:</p>
  
  <pre><code>Drawable drawable = new BitmapDrawable(bitmap);
  </code></pre>
  
  <p>but according to the code context, the bitmap should't be null or recycled, that'really strange.<br /><br />
  I have been hanging around this issue nearly two days and can't figure it out what the problem is, but the issue does exists. Could anybody give me some suggestions? Any response is appreciated! Thanks！<br /><br /></p>
  
  <p>By the way, when I try to the RuntimeException like this, this kind of exception can't be catched, WHY?</p>
  
  <pre><code>try
  {
      bitmap.recycle();
      Drawable drawable = new BitmapDrawable(bitmap);
      fristFrame.setBackgroundDrawable(drawable);
  }
  catch (RuntimeException e)
  {
      e.printStackTrace();
  }
  </code></pre>