我正在做一个camara项目,其中一个活动的捕获图像被发送到另一个活动。问题是,当我点击捕获按钮时,应用程序崩溃并在logcat上显示空点异常
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 :(得分:1)
除非您为图像选择了低分辨率,否则会因为事务太大的异常而崩溃。 IPC交易的大小限制为1MB,这意味着Intent
的大小限制为~1MB。大多数相机照片都会远远大于此。
您的选择是:
拍摄低分辨率图像
摆脱第二项活动并在一项活动中完成所有这项工作(例如,使用多个片段)
小心通过其他方式(例如,单身)从第一个活动到第二个活动获取图像,采取适当的步骤以避免令人讨厌的内存泄漏
答案 1 :(得分:0)
这是您的代码崩溃的部分:
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
让我们看看它来自哪里。您有四个值:
mCamera
:如果mCamera
为null
,则会抛出NullPointerException
,因为您尝试使用未存在对象的takePicture
方法null
。如果takePicture
尝试使用这些参数的数据成员或方法,则会引发NullPointerException
mPicture
可能是null
。如果mPicture
为null
并且您尝试在takePicture
中使用其中一个数据成员/方法,则会引发NullPointerException
我相信mCamera
是null
。如果我是对的,您应该查看如何创建相机对象:
/**
* 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
,你很可能会意识到你的问题是什么。