0

我一直在尝试寻找类似的错误,但我遇到了困难……我还是 android 开发的新手,但希望有人能指出一些方向。

我有一个可以预览的相机应用程序,但是当我尝试单击按钮拍照时,我的应用程序崩溃了。有人能帮我吗?

[PhotoActivity.java]

public class PhotoActivity extends Activity {
    public static final int MEDIA_TYPE_IMAGE = 1;

    protected static final String TAG = "Activity";
    private Camera mCamera;
    private CameraPreview mCameraPreview;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);

        mCamera = getCameraInstant();
        mCameraPreview = new CameraPreview(this, mCamera);

        FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
        preview.addView(mCameraPreview);

        // Add a listener to the Capture button
        Button captureButton = (Button) findViewById(id.button_capture);
        captureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // get an image from the camera
                    Log.e("log", "mPicture"+mPicture);

                    mCamera.takePicture(null, null, mPicture);
                }
            }
        );  
    }

    PictureCallback mPicture = new PictureCallback(){
     @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if(pictureFile==null){

                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());

            }
        }

    };



    /**
     * Helper method to access the camera returns null if
     * it cannot get the camera or does not exist
     * @return
     */
    private Camera getCameraInstant(){
        Camera camera = null;

        try{
            camera=Camera.open();
        }catch (Exception e){
            // cannot get camera or does not exist
        }
        return camera;
    }

    /** Create a File for saving the image */
    private File getOutputMediaFile(int type){

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");

        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");

        return mediaFile;
    }

}

对不起所有的编码,但我真的需要一些帮助......在此先感谢。

更新

[CameraPreview.java]

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "Preview";
    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;

    //Constructor that obtains context and camera
    public CameraPreview(Context context, Camera camera) {
        super(context);
        //this.mCamera = camera;
        this.mCamera = camera;
        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mCamera.release();
        mCamera = Camera.open();

        try {
            mCamera.setPreviewDisplay(surfaceHolder);

            mCamera.setDisplayOrientation(90);

            mCamera.startPreview();
        } catch (IOException e) {
          // left blank for now
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());

        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if (mCamera != null) {
            Log.d(TAG,"Stopping preview in SurfaceDestroyed().");
            mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
            int width, int height) {


        if(mSurfaceHolder.getSurface()==null){
            //preview surface does not exist
            return;
        }
        try {
            mCamera.stopPreview();
        }catch(Exception e){
            //ignore: tried to stop a non-existent preview
        }

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mSurfaceHolder);
            mCamera.setDisplayOrientation(90);
            mCamera.startPreview();
        } catch (Exception e) {
            // intentionally left blank for a test
            Log.d(TAG, "Error starting camera preview: "+e.getMessage());
        }
    }

}

错误

05-09 21:19:29.013: E/AndroidRuntime(3823): FATAL EXCEPTION: main
05-09 21:19:29.013: E/AndroidRuntime(3823): java.lang.RuntimeException: Method called after release()
05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.native_takePicture(Native Method)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.takePicture(Camera.java:746)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.hardware.Camera.takePicture(Camera.java:710)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.liu.photo.PhotoActivity$2.onClick(PhotoActivity.java:73)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.view.View.performClick(View.java:2486)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.view.View$PerformClick.run(View.java:9130)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Handler.handleCallback(Handler.java:587)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Handler.dispatchMessage(Handler.java:92)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.os.Looper.loop(Looper.java:130)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at android.app.ActivityThread.main(ActivityThread.java:3703)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at java.lang.reflect.Method.invoke(Method.java:507)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    05-09 20:21:01.214: E/AndroidRuntime(2813):     at dalvik.system.NativeStart.main(Native Method)

这就是堆栈跟踪吗?我很新,还在学习这些术语。谢谢

4

2 回答 2

1

从方法中删除这两行surfaceCreated

    mCamera.release();
    mCamera = Camera.open();

您已经Camera在您的 中打开了该对象Activity,无需释放它并重新打开它。

编辑您实际上应该删除整个实现,surfaceCreated并将实现留空。你只是在重复你已经做过的事情surfaceChanged,无论如何这是实现它的重要地方。

于 2012-05-10T04:30:34.620 回答
-1

请参阅此页面: http: //developer.android.com/reference/android/hardware/Camera.html 并确保您遵守规则。特别要注意,在拍照之前必须开始预览。

于 2012-05-10T04:01:14.923 回答