按下按钮拍照时native_takePicture()错误

时间:2012-05-10 03:45:01

标签: java android sdk camera android-camera

我一直在努力寻找类似的错误,但我很难...我仍然是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)

这是堆栈跟踪的内容吗?我很新,还在学习这些条款。谢谢

2 个答案:

答案 0 :(得分:1)

surfaceCreated方法中删除这两行:

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

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

编辑您实际上应该删除surfaceCreated的整个实现,并将实现留空。你只是重复你在surfaceChanged中已经完成的事情,这是实现它的重要场所。

答案 1 :(得分:-1)

请看这个页面:  http://developer.android.com/reference/android/hardware/Camera.html 并确保遵守规则。 特别注意在拍照前必须先开始预览。