質問

I am having a weird issue. I am writing an app that continues taking pics.The app was working fine, however it doesn't start after implementing the onPause method.I checked and it is entering the onPause once it is created. Here is my code:

public class MyCamera extends Activity {
    private Camera mCamera;
    private CameraPreview mCameraPreview;
    private FrameLayout preview;
    /* Remaining stuff: 
        1. Implement Onpause to release camera and onResume reopen it
        2. Save timer to stop it ion onpauseoonPause()  
        3. rotate images
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mCamera = getCameraInstance();
        mCameraPreview = new CameraPreview(this, mCamera);
        mCamera.setDisplayOrientation(90);
        //FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

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

                // 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds


                    /*new CountDownTimer(5000,1000){

                        @Override
                        public void onFinish() {

                        }

                        @Override
                        public void onTick(long millisUntilFinished) {
                            mCamera.takePicture(null, null, mPicture);

                        }

                    }.start();*/


                    Timer timer = new Timer();
                    timer.schedule(new TimerTask()
                    {
                        @Override
                        public void run()
                        {
                            mCamera.startPreview();
                            mCamera.takePicture(null, null, mPicture);
                            try {
                                Thread.sleep(500);
                            } catch(InterruptedException ex) {
                                Thread.currentThread().interrupt();
                            }
                            mCamera.startPreview();                                   
                        }
                    }, 0, 5000);

            }

        });
    }

    /**
     * Helper method to access the camera returns null if it cannot get the 
     * camera or does not exist
     * 
     * @return
     */
    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                //createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter);
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }

    };

    private static File getOutputMediaFile() {
        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;
    }



    @Override
    public void onPause() {
        super.onPause();  // Always call the superclass method first
        Log.d("LifeCycle", "In the onPause() event");
        // Release the Camera because we don't need it when paused
        // and other activities might need to use it.
        preview.removeAllViews();
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }

    }

    /*@Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        setContentView(R.layout.main);
        // Get the Camera instance as the activity achieves full user focus
        if (mCamera == null) {
            mCamera = getCameraInstance();
            mCameraPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            preview.addView(mCameraPreview);
            mCamera.startPreview();

        }
    }*/ 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_camera, menu);
        return true;
    }

}

Here's the LogCat window:

04-17 14:44:11.954: I/dalvikvm(7444): Debugger is active
04-17 14:44:12.104: I/System.out(7444): Debugger has connected
04-17 14:44:12.104: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.314: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.514: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.714: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.914: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.114: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.314: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.514: I/System.out(7444): debugger has settled (1453)
04-17 14:44:13.784: D/dalvikvm(7444): Note: class Lcom/lge/mdm/manager/ILGMDMDevicePolicyManager$Stub; has 319 unimplemented (abstract) methods
04-17 14:44:14.274: D/LifeCycle(7444): In the onPause() event
04-17 14:44:14.364: I/Adreno-EGL(7444): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build:  ()
04-17 14:44:14.364: I/Adreno-EGL(7444): OpenGL ES Shader Compiler Version: E031.24.00.02
04-17 14:44:14.364: I/Adreno-EGL(7444): Build Date: 01/20/14 Mon
04-17 14:44:14.364: I/Adreno-EGL(7444): Local Branch: PMH2-KK_3.5-RB1-AU61-554722-586267-set2
04-17 14:44:14.364: I/Adreno-EGL(7444): Remote Branch: 
04-17 14:44:14.364: I/Adreno-EGL(7444): Local Patches: 
04-17 14:44:14.364: I/Adreno-EGL(7444): Reconstruct Branch: 
04-17 14:44:14.394: D/OpenGLRenderer(7444): Enabling debug mode 0
04-17 14:44:14.494: I/ActivityManager(7444): Timeline: Activity_idle id: android.os.BinderProxy@428b55e0 time:4511859
04-17 14:44:14.504: I/ActivityManager(7444): Timeline: Activity_idle id: android.os.BinderProxy@428b55e0 time:4511861

Any ideas why? Thanks

役に立ちましたか?

解決

onPause ():

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

Possible causes---

  • You are somehow calling some other activity that has kept the current activity in background. Check your code at various points.

  • You have somehow killed the current activity by itself and so this is called just before the activity is marked as being killed.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top