0

我是android编程的新手。我只想在android中制作自定义相机。为了做到这一点,我使用了这个网站(http://developer.android.com/guide/topics/media/camera.html)虽然我按照网站中的步骤操作,但我收到一个错误“不幸的是你的应用程序已停止”。我搜索了有关“不幸已停止”的所有问题,并在此处阅读了答案。但他们没有帮助我。请帮我!

这是我的cameraActivity代码:

public class CameraActivity extends Activity {

protected static final String TAG = null;
protected static final String MEDIA_TYPE_IMAGE = ".jpeg";
private static Camera mCamera;
private CameraPreview mPreview;

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

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById (R.id.camera_preview);
    preview.addView(mPreview);


    final PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Throwable e = null;
                Log.d(TAG, "Error creating media file, check storage permissions: " +
                    e.getMessage());
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }

        private File getOutputMediaFile(String mediaTypeImage) {
            // TODO Auto-generated method stub
            return null;
        }
    };

Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }});}

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}}

当我在 logcat 中单击错误时,它向我显示了这个

“mcamera.setPreviewDisplay(持有人);”
在这段代码中:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {


private SurfaceHolder mHolder;
private Camera mcamera;

public CameraPreview(Context context,Camera camera){
    super(context);
    mcamera=camera;
    mHolder=getHolder();
    mHolder.addCallback(this);
    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 {
        mcamera.setPreviewDisplay(holder);
        mcamera.startPreview();
    } catch (IOException e) {
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}

在 LogCat 中

E/AndroidRuntime(1071):致命异常:主要
 E/AndroidRuntime(1071): java.lang.NullPointerException
 E/AndroidRuntime(1071):在 com.example.ntry.CameraPreview.surfaceCreated(CameraPreview.java:32)
 E/AndroidRuntime(1071):在 android.view.SurfaceView.updateWindow(SurfaceView.java:569)
 E/AndroidRuntime(1071): 在 android.view.SurfaceView.access$000(SurfaceView.java:86)
 E/AndroidRuntime(1071):在 android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
 E/AndroidRuntime(1071):在 android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
 E/AndroidRuntime(1071):在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
 E/AndroidRuntime(1071):在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
 E/AndroidRuntime(1071):在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
 E/AndroidRuntime(1071):在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 E/AndroidRuntime(1071):在 android.view.Choreographer.doCallbacks(Choreographer.java:562)
 E/AndroidRuntime(1071):在 android.view.Choreographer.doFrame(Choreographer.java:532)
 E/AndroidRuntime(1071):在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 E/AndroidRuntime(1071): 在 android.os.Handler.handleCallback(Handler.java:725)
 E/AndroidRuntime(1071):在 android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime(1071): 在 android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(1071): 在 android.app.ActivityThread.main(ActivityThread.java:5041)
 E/AndroidRuntime(1071): 在 java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1071):在 java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(1071):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 E/AndroidRuntime(1071):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 E/AndroidRuntime(1071): 在 dalvik.system.NativeStart.main(Native Method)

4

2 回答 2

0

如果它发生在您的应用程序第一次启动时,那么您的相机对象为 Null 或您的持有者为 Null

但是,如果在您第二次导航到相机视图时发生这种情况,那么您的相机将保留在前一个视图中并且第二次无法访问,那么您必须在暂停视图或破坏视图时释放相机

希望它会有所帮助

于 2013-07-04T07:50:59.443 回答
0

从这里看起来getCameraInstance返回null。是否Camera.open()引发异常?

于 2013-07-04T07:55:05.670 回答