Android代码未执行

时间:2014-10-22 12:35:16

标签: android multithreading

我正在制作一款可以显示预览和拍照的Android相机应用。此活动中有两个按钮:启动预览并停止预览。当用户点击停止预览按钮时,该应用程序将捕获照片。但是,当我测试我的应用程序时,与停止预览相关联的代码似乎被跳过而不执行。以下是我的代码:

SuppressWarnings("deprecation")
public class CameraOpened extends Activity implements SurfaceHolder.Callback{

     Camera camera;
     SurfaceView surfaceView;
     SurfaceHolder surfaceHolder;
     boolean previewing = false;
     static final int REQUEST_IMAGE_CAPTURE = 1;
     int count;
     PictureCallback rawCallback;
     ShutterCallback shutterCallback;
     PictureCallback jpegCallback;


     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_opened);

           Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
           Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);

           getWindow().setFormat(PixelFormat.UNKNOWN);
           surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
           surfaceHolder = surfaceView.getHolder();
           surfaceHolder.addCallback(this);
           surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

           shutterCallback = new ShutterCallback() {
                 public void onShutter() {
                     Log.i("Log", "onShutter'd");
                 }
             };

             rawCallback = new PictureCallback() {
                 public void onPictureTaken(byte[] data, Camera camera) {
                     Log.d("Log", "onPictureTaken - raw");
                 }
             };

             jpegCallback = new PictureCallback() {
                 public void onPictureTaken(byte[] data, Camera camera) {
                     FileOutputStream outStream = null;
                     try {
                         String dir;
                         dir = Environment.getExternalStorageDirectory().toString() + "/DCIM/"; 
                         File newdir = new File(dir); 
                         newdir.mkdirs();

                         File imageFileName = new File(newdir, "crop_"+ ".jpg");
                         outStream = new FileOutputStream(imageFileName);
                         outStream.write(data);
                         outStream.close();
                         Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                     } catch (FileNotFoundException e) {
                         e.printStackTrace();
                     } catch (IOException e) {
                         e.printStackTrace();
                     } finally {
                     }
                     Log.d("Log", "onPictureTaken - jpeg");
                 }
             };

           buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){

               public void onClick(View v) {

                // TODO Auto-generated method stub
                if(!previewing){
                 camera = Camera.open();
                 camera.setDisplayOrientation(90);
                 if (camera != null){
                  try {
                   camera.setPreviewDisplay(surfaceHolder);
                   camera.startPreview();
                   previewing = true;
                  } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                  }
                 }
                }
               }});

           buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){

               @Override
               public void onClick(View v) {
                // TODO Auto-generated method stub
                if(camera != null && previewing){

                 camera.takePicture(shutterCallback, rawCallback, jpegCallback);
                 previewing = false;


                 camera.stopPreview();
                 camera.release();
                 camera = null;
                }
               }});
    }

     public void savePhoto(Bitmap bmp)
     {
         String dir;
     dir = Environment.getExternalStorageDirectory().toString() + "/DCIM/"; 
     File newdir = new File(dir); 
     newdir.mkdirs();
     FileOutputStream out = null;

     //file name
     Calendar c = Calendar.getInstance();
     String date = fromInt(c.get(Calendar.MONTH))
                 + fromInt(c.get(Calendar.DAY_OF_MONTH))
                 + fromInt(c.get(Calendar.YEAR))
                 + fromInt(c.get(Calendar.HOUR_OF_DAY))
                 + fromInt(c.get(Calendar.MINUTE))
                 + fromInt(c.get(Calendar.SECOND));
     File imageFileName = new File(newdir, "crop_"+ ".jpg");

     try
     {
      out = new FileOutputStream(imageFileName);
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
      out.flush();
      out.close();
      out = null;
     } catch (Exception e)
     {
     e.printStackTrace();
     }
     }

     public String fromInt(int val)
     {
     return String.valueOf(val);
     }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.camera_opened, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
     public void surfaceChanged(SurfaceHolder holder, int format, int width,
       int height) {
      // TODO Auto-generated method stub

     }

     @Override
     public void surfaceCreated(SurfaceHolder holder) {
      // TODO Auto-generated method stub

     }

     @Override
     public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub

     }
     private void captureImage() {
            // TODO Auto-generated method stub
            //camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }


}

以下是我的LogCat:

10-22 20:28:38.218: D/Camera(14375): [CTA] Camera open urrentpackagename = com.camera
10-22 20:28:38.218: D/Camera(14375): [CTA] Camera open application != null
10-22 20:28:38.218: D/Camera(14375): [CTA] check CTA permisson mAllowUsing = true
10-22 20:28:39.258: I/Choreographer(14375): Skipped 61 frames!  The application may be doing too much work on its main thread.
10-22 20:28:49.318: D/Camera(14375): [CTA] Camera open urrentpackagename = com.camera
10-22 20:28:49.318: D/Camera(14375): [CTA] Camera open application != null
10-22 20:28:49.318: D/Camera(14375): [CTA] check CTA permisson mAllowUsing = true
10-22 20:28:53.028: I/Choreographer(14375): Skipped 77 frames!  The application may be doing too much work on its main thread.

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

使用背景线程进行相机操作我希望它会有所帮助 参考#AsyncTask Android Guide