2016-03-10 78 views
-3

我正在做一个camara项目,其中一个活动捕获的图像被发送到另一个活动。问题是,当我在拍摄按钮,单击该应用程序崩溃,并显示在Camara应用程序崩溃点击

public class MainActivity extends Activity { 

    private Camera mCamera; 
    private CameraPreview mPreview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mCamera = getCameraInstance(); 



     // Create the Camera preview view 
     mPreview = new CameraPreview(this, mCamera); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
     preview.addView(mPreview); 

     final Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

      @Override 
      public void onPictureTaken(byte[] data, Camera camera) { 
       Intent intent = new Intent(MainActivity.this, EditActivity.class); 
       intent.putExtra(EditActivity.EXTRA_BYTES, data); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
      } 
     }; 

     // Add a listener to the capture button 
     ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture); 
     captureButton.setImageDrawable(getResources().getDrawable(R.drawable.cam)); 
     captureButton.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         mCamera.takePicture(null, null, mPicture); 
        } 
       } 

     ); 


    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     finish(); 
     mPreview.stopPreview(); 
    } 

    /** 
    * A safe way to get an instance of the Camera object. 
    * 
    * Returns null if camera is unavailable. 
    */ 
    private static Camera getCameraInstance() { 
     Camera c = null; 
     try { 
      c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
     } catch (Exception ignore) { 
      // Camera is not available (in use or does not exist). Do nothing. 
     } 
     return c; 
    } 

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 

     private SurfaceHolder mHolder; 
     private Camera mCamera; 

     public CameraPreview(Context context, Camera camera) { 
      super(context); 
      mCamera = camera; 

      // Install a SurfaceHolder.Callback so we get notified when the 
      // underlying surface is created and destroyed. 
      mHolder = getHolder(); 
      mHolder.addCallback(this); 
      // deprecated setting, but required on Android versions prior to 3.0 
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     } 

     public void surfaceCreated(SurfaceHolder holder) { 
      // The Surface has been created, now tell the camera where to draw the preview. 
      try { 
       if (mCamera != null) { 
        mCamera.setDisplayOrientation(90); 
        mCamera.setPreviewDisplay(holder); 
        mCamera.startPreview(); 
       } 
      } catch (IOException ignore) { 
       // Do nothing 
      } 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
      if (mCamera != null) { 
       mCamera.stopPreview(); 
      } 
     } 

     private void stopPreview() { 
      if (mCamera != null) { 
       mCamera.stopPreview(); 
       mCamera.release(); 
       mCamera = null; 
      } 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
      // If your preview can change or rotate, take care of those events here. 
      // Make sure to stop the preview before resizing or reformatting it. 

      if (mHolder.getSurface() == null) { 
       // preview surface does not exist 
       return; 
      } 

      // Stop preview before making changes 
      try { 
       mCamera.stopPreview(); 
      } catch (Exception ignore) { 
       // Tried to stop a non-existent preview. Do nothing. 
      } 

      // Set preview size and make any resize, rotate or reformatting changes here 

      // Start preview with new settings 
      try { 
       mCamera.setPreviewDisplay(mHolder); 
       mCamera.startPreview(); 
      } catch (Exception ignore) { 
       // Do nothing 
      } 
     } 
    } 
} 
+0

请提供堆栈跟踪和确切的异常 – NightFury

+0

“_and在logcat_上显示空值点异常”当在此处提问时,最好_实际显示LogCat和堆栈跟踪。 – JonasCz

回答

1

除非你选择的图像分辨率低,你是一个交易过大的异常崩溃的logcat的零点例外。 IPC交易规模有1MB限制,这意味着您的Intent的规模限制为1MB。大多数相机的照片将远远大于此。

的选项有:

  1. 以低分辨率图像

  2. 摆脱次活动,并完成所有的工作,在一个活动(例如,使用多个片段)

  3. 小心通过一些其他方式(例如,单身)从第一个活动获取图像,采取适当的步骤避免讨厌的内存泄漏

0

这是部分在您的代码崩溃:

   @Override 
       public void onClick(View v) { 
        mCamera.takePicture(null, null, mPicture); 
       } 

让我们来看看它在哪里可以从何而来。你有四个值:

  • mCamera:如果mCameranull,然后NullPointerException被抛出,因为你尝试使用unexisting对象
  • 第一和第二个参数是nulltakePicture方法。如果takePicture试图使用这些参数的数据成员或方法,则引发NullPointerException
  • mPicture可能是null。如果mPicturenull并尝试使用其数据成员/方法中的一种内takePicture,然后NullPointerException抛出

我相信mCameranull。如果我是正确的,你应该检讨你如何建立你的相机对象:

/** 
* A safe way to get an instance of the Camera object. 
* 
* Returns null if camera is unavailable. 
*/ 
private static Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
    } catch (Exception ignore) { 
     // Camera is not available (in use or does not exist). Do nothing. 
    } 
    return c; 
} 

在这里,你隐藏任何Exception。我想恳请您调试您的项目,看看这里是否有Exception。如果你看到给定的Exception,你很可能会意识到你的问题是什么。

+0

耶正是那里的应用程序崩溃。解决办法是什么? – Yash

+0

@Yash,解决方案是找出导致NullPointerException的原因。执行时mCamera为空吗?如果是这样,找出忽略的性质(你忽略的例外) –

相关问题