【发布时间】:2014-04-17 11:39:29
【问题描述】:
我遇到了一个奇怪的问题。我正在编写一个继续拍照的应用程序。该应用程序运行良好,但是在实现 onPause 方法后它没有启动。我检查并在创建后进入 onPause。这是我的代码:
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;
}
}
这是 LogCat 窗口:
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
任何想法为什么? 谢谢
【问题讨论】: