title: Android SurfaceView IllegalArgumentException lockCanvas
body: <p>I'm working on a video into a <a href="http://developer.android.com/reference/android/view/SurfaceView.html" rel="nofollow">SurfaceView</a>.
  My goal is to get recurrent bitmaps of the running video.
  Here is my Custom implementation:</p>
  
  <pre><code>private static final String TAG = "XXX";
  private Activity activity;
  private SurfaceHolder mSurface;
  private MediaPlayer mMediaPlayer;
  private SurfaceHolder mActiveSurface;
  public ImageView imageView;
  
  boolean locked, locked1;
  private boolean isCreated;
  
  
  public AlphaSurfaceView(Context context,Activity activity) {
      super(context);
      getHolder().addCallback(this);
      setWillNotDraw(false);
      this.activity = activity;
  }
  
  public AlphaSurfaceView(Context context, AttributeSet attrs) {
      super(context, attrs);
  
  }
  
  public AlphaSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
  }
  
  
  @Override
  public void draw(Canvas canvas) {
      super.draw(canvas);
  }
  
  @Override
  public void onDraw(Canvas canvas) {
      //super.onDraw(canvas);
      if(isCreated){
          if(getHolder() != null &amp;&amp; getHolder().getSurface().isValid()){
              Canvas c = null;
              try {
                  if(!locked){
                      try {
                          c = getHolder().lockCanvas();
                          locked = true;
                      }catch (IllegalArgumentException e){
                          e.printStackTrace();
                          locked = false;
                      }
                      BitmapDrawable bdrawable = new BitmapDrawable();
                      bdrawable.draw(c);
                  }
  
              }catch (Exception e){
                  e.printStackTrace();
                  locked = false;
              }finally {
                  if(c != null &amp;&amp; locked){
                      getHolder().unlockCanvasAndPost(c);
                      locked = false;
                  }
              }
          }
      }else{
          super.onDraw(canvas);
      }
      invalidate();
  
  }
  
  
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      mSurface = holder;
      mMediaPlayer = MediaPlayer.create(getContext(), Uri.parse("XX"), mSurface);
      mActiveSurface = mSurface;
      try {
          mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
              @Override
              public void onPrepared(MediaPlayer mp) {
                  mMediaPlayer.start();
                  mMediaPlayer.setLooping(true);
              }
          });
  
      } catch (Exception e) {
          e.printStackTrace();
      }
      this.isCreated = true;
  
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      Log.d(TAG, "surfacechanged");
  
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      mMediaPlayer.stop();
  }
  
  @Override
  protected void onDetachedFromWindow() {
      super.onDetachedFromWindow();
      mMediaPlayer.stop();
  }
  </code></pre>
  
  <p>The problem is an IllegalArgumentException occured by the lockCanvas() method.</p>
  
  <p>I tried many possibilities as:</p>
  
  <ul>
  <li>Adding some boolean (locked &amp; creation of surfaceview)</li>
  <li>Test if surface is valid</li>
  <li>even add exported="true" in targeted activity in Manifest's xml</li>
  </ul>
  
  <p>By the way the first idea was to use getDrawingCache() but even i added setCacheEnabled(true), the return was null.</p>
  
  <p>So how to resolve this Exception or using another way to get each frame?</p>
  
  <p>Thanks!</p>