2014-07-07 32 views
0

我有代码拍摄的图片,但它得到PictureCallback()后,它继续在代码方面徘徊,我最终在我有我的字节之前执行其他代码阵列。有什么办法可以制止这个吗?它只需要几毫秒,我不认为我会介意停止UI线程。Android的PictureCallback需要先执行然后继续到其他代码

private static byte[] patronImage; 
. 
. 
. 
takePatronPicture(); 
//I want to do something with patronImage here, but the next line 
//gets executed almost immediately while getJpegCallback is still 
//doing its thing. 
code that does something with patronImage; 
. 
. 
. 
public void takePatronPicture(){ 
     if (camera != null) { 
      try { 
       SurfaceView surfaceView = (SurfaceView)getView().findViewById(R.id.surfaceView1); 
       camera.setPreviewDisplay(surfaceView.getHolder());  
       camera.startPreview(); 
       camera.takePicture(null, null, getJpegCallback); 
      } 
      catch(Exception e) { 
       e.printStackTrace(); 
      }   
     } 
    } 

    final PictureCallback getJpegCallback = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      try { 
       patronImage = data; 

       camera.release(); 
       camera = null; 
      } catch (final Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 

回答

1

回调的目的是当它完成后它会打电话给你。如果您有其他需要操作数据的代码运行,则将其放入回调中。您不会冻结当前正在等待的线程。事实上,如果操作将事件发布到主线程的活套,可能会导致死锁。

+0

谢谢。有时候我看不到树木的阿甘。 – Clutch

相关问题