我的Activity课程在这里:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// startCamera();
}
public void startCamera() {
setContentView(R.layout.camera_view);
mCamera = getCameraInstance();// Open Camera
mPreview = new CameraPreview(this,mCamera);// Goto Another Class
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.setLayoutParams(new FrameLayout.LayoutParams(400,400));
//Declare Frame in which camera will be opened
preview.addView(mPreview); // show this class into frame
}
protected void onResume() {
super.onResume();
Log.d("Print","resume()");
startCamera();
}
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
Toast.makeText(null,"No camera on this device",Toast.LENGTH_LONG).show();
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/*
protected void onPause() {
if (mCamera!=null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
super.onPause();
}
*/
}
和Camera Preview课程在这里:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
@SuppressWarnings("deprecation")
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) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("Print","Error setting camera preview: "+e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("Print","Destroyed()");
mCamera.stopPreview();
mCamera.release();
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface()==null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("Print","Error starting camera preview: "+e.getMessage());
}
}
}
然而,当我测试类时,onResume()似乎最初被调用,然后在1或2秒后再次被调用。所以相机必须再次刷新。如果我根本没有onResume(),相机预览是稳定的,但如果我从主屏幕或其他应用程序再次切换到应用程序,则会崩溃。我发现onPause()不会影响其中任何一个。 我的代码是否正确?应该添加/删除什么才能使其不再刷新,并且在应用切换后仍然不会崩溃?
这是从第1个onResume()到另一个的LogCat输出:
12-19 22:58:27.604: D/Print(28831): resume()
12-19 22:58:27.624: D/dalvikvm(29223): GC_CONCURRENT freed 195K, 8% free 3591K/3864K, paused 2ms+1ms, total 9ms
12-19 22:58:27.634: D/dalvikvm(29223): GC_FOR_ALLOC freed 245K, 10% free 3667K/4068K, paused 6ms, total 7ms
12-19 22:58:27.644: D/dalvikvm(29223): GC_FOR_ALLOC freed 241K, 11% free 3742K/4164K, paused 6ms, total 7ms
12-19 22:58:27.649: I/AwesomePlayer(13841): setDataSource_l(URL suppressed)
12-19 22:58:27.659: I/AwesomePlayer(13841): setDataSource_l(URL suppressed)
12-19 22:58:27.674: D/dalvikvm(29223): GC_CONCURRENT freed 255K, 8% free 3908K/4240K, paused 1ms+0ms, total 9ms
12-19 22:58:27.674: I/Camera2ClientBase(13841): Camera 0: Opened
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): >>> I'm Samsung's CameraHAL_2(ID:0) <<<
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): g_cam2_device : 0x00000000
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): (ExynosCameraHWInterface2): ENTER
12-19 22:58:27.789: D/dalvikvm(29223): GC_CONCURRENT freed 210K, 6% free 4151K/4416K, paused 2ms+7ms, total 50ms
12-19 22:58:27.854: D/dalvikvm(29223): GC_CONCURRENT freed 281K, 8% free 4336K/4676K, paused 1ms+1ms, total 11ms
12-19 22:58:27.879: D/ExynosCameraHAL2(13841): (ExynosCameraHWInterface2): EXIT
12-19 22:58:27.904: D/gamook(29223): Loaded 66 configuration entries.
12-19 22:58:27.914: D/dalvikvm(29223): GC_CONCURRENT freed 399K, 10% free 4447K/4908K, paused 1ms+4ms, total 27ms
12-19 22:58:27.914: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 1ms
12-19 22:58:27.934: D/mali_winsys(28831): new_window_surface returns 0x3000
12-19 22:58:27.964: D/ExynosCameraHAL2(13841): (allocateStream): stream width(1920) height(1080) format(22)
12-19 22:58:27.969: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(0), num_buff(8), handle(b8683a48)
12-19 22:58:27.974: D/ExynosCameraHAL2(13841): (allocateStream): stream width(2560) height(1920) format(21)
12-19 22:58:28.009: D/dalvikvm(29223): GC_CONCURRENT freed 279K, 8% free 4608K/4988K, paused 2ms+2ms, total 22ms
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(4), num_buff(6), handle(b85aabd8)
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (allocateStream): stream width(2560) height(1920) format(ffffffff)
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (allocateStream): jpeg stream exists
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): START stream thread 1 release 1869
12-19 22:58:28.049: D/ExynosCameraHAL2(13841): END stream thread 1 release 1874
12-19 22:58:28.064: D/dalvikvm(29223): GC_CONCURRENT freed 320K, 8% free 4694K/5076K, paused 3ms+2ms, total 18ms
12-19 22:58:28.139: D/dalvikvm(29223): GC_CONCURRENT freed 452K, 10% free 4697K/5212K, paused 3ms+2ms, total 30ms
12-19 22:58:28.184: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(5), num_buff(11), handle(b855dc30)
12-19 22:58:28.199: D/dalvikvm(29223): GC_CONCURRENT freed 433K, 10% free 4775K/5268K, paused 2ms+2ms, total 29ms
12-19 22:58:28.199: D/ExynosCameraHAL2(13841): (allocateReprocessStreamFromStream): output_stream_id(5)
12-19 22:58:28.249: D/dalvikvm(29223): GC_CONCURRENT freed 513K, 11% free 4828K/5400K, paused 1ms+1ms, total 16ms
12-19 22:58:28.249: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 5ms
12-19 22:58:28.294: D/dalvikvm(29223): GC_CONCURRENT freed 528K, 11% free 4884K/5472K, paused 2ms+0ms, total 17ms
12-19 22:58:28.294: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 3ms
12-19 22:58:28.344: D/dalvikvm(29223): GC_CONCURRENT freed 601K, 12% free 4888K/5548K, paused 2ms+1ms, total 19ms
12-19 22:58:28.344: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 7ms
12-19 22:58:28.389: D/dalvikvm(29223): GC_CONCURRENT freed 616K, 13% free 4857K/5548K, paused 0ms+1ms, total 17ms
12-19 22:58:28.399: D/ExynosCameraHAL2(13841): ### Applying AF Mode change(Mode 2)
12-19 22:58:28.404: D/dalvikvm(29223): GC_FOR_ALLOC freed 17K, 13% free 4843K/5548K, paused 14ms, total 14ms
12-19 22:58:28.404: I/dalvikvm-heap(29223): Grow heap (frag case) to 4.823MB for 39033-byte allocation
12-19 22:58:28.419: D/dalvikvm(29223): GC_FOR_ALLOC freed <1K, 13% free 4881K/5588K, paused 15ms, total 15ms
12-19 22:58:28.434: D/dalvikvm(29223): GC_FOR_ALLOC freed <1K, 13% free 4881K/5588K, paused 19ms, total 19ms
12-19 22:58:28.439: I/dalvikvm-heap(29223): Grow heap (frag case) to 4.897MB for 78050-byte allocation
12-19 22:58:28.454: D/ExynosCameraHAL2(13841): DEBUG(m_mainThreadFunc)(0x10): No more service requests left in the queue
12-19 22:58:28.459: D/dalvikvm(29223): GC_FOR_ALLOC freed 0K, 13% free 4957K/5668K, paused 24ms, total 24ms
12-19 22:58:28.529: D/dalvikvm(29138): GC_CONCURRENT freed 359K, 11% free 3745K/4168K, paused 1ms+1ms, total 13ms
12-19 22:58:28.559: D/dalvikvm(29113): GC_CONCURRENT freed 378K, 11% free 3991K/4436K, paused 2ms+1ms, total 14ms
12-19 22:58:28.634: D/dalvikvm(29223): GC_CONCURRENT freed 463K, 12% free 5023K/5668K, paused 3ms+2ms, total 20ms
12-19 22:58:28.639: I/Choreographer(28831): Skipped 43 frames! The application may be doing too much work on its main thread.
12-19 22:58:28.704: I/ActivityManager(447): Process com.google.android.apps.plus (pid 28888) has died.
12-19 22:58:28.719: I/ActivityManager(447): Process com.google.process.gapps (pid 28963) has died.
12-19 22:58:28.734: I/ActivityManager(447): Displayed com.example.dif/.CameraActivity: +1s138ms
12-19 22:58:28.804: I/ActivityManager(447): Process com.facebook.katana:dash (pid 29014) has died.
12-19 22:58:28.864: I/ActivityManager(447): Start proc com.estrongs.android.pop for broadcast com.estrongs.android.pop/.app.InstallMonitorReceiver: pid=29263 uid=10096 gids={50096, 3003, 3002, 3001, 1028, 1015}
12-19 22:58:28.989: W/System.err(29263): java.lang.NoSuchMethodException: setCompatibilityInfo [class android.content.res.CompatibilityInfo$1]
12-19 22:58:28.994: W/System.err(29263): at java.lang.Class.getConstructorOrMethod(Class.java:472)
12-19 22:58:28.994: W/System.err(29263): at java.lang.Class.getMethod(Class.java:857)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.util.af.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.util.af.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.esclasses.i.<init>(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.esclasses.i.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.FexApplication.getResources(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.ui.b.a.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.FexApplication.onCreate(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-19 22:58:28.994: W/System.err(29263): at android.os.Handler.dispatchMessage(Handler.java:102)
12-19 22:58:28.994: W/System.err(29263): at android.os.Looper.loop(Looper.java:136)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-19 22:58:28.994: W/System.err(29263): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 22:58:28.994: W/System.err(29263): at java.lang.reflect.Method.invoke(Method.java:515)
12-19 22:58:28.994: W/System.err(29263): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-19 22:58:28.994: W/System.err(29263): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-19 22:58:28.994: W/System.err(29263): at dalvik.system.NativeStart.main(Native Method)
12-19 22:58:28.994: E/ObjectHelper(29263): Can't find method:setCompatibilityInfo
12-19 22:58:29.034: I/ActivityManager(447): Process com.google.android.gms (pid 29003) has died.
12-19 22:58:29.044: D/dalvikvm(29263): GC_CONCURRENT freed 244K, 9% free 3311K/3616K, paused 2ms+1ms, total 18ms
12-19 22:58:29.059: I/ActivityManager(447): START u0 {flg=0x10800000 cmp=com.estrongs.android.pop/.app.InstallMonitorActivity (has extras)} from pid 29263
12-19 22:58:29.104: I/ActivityManager(447): Start proc com.metago.astro for broadcast com.metago.astro/com.kii.cloud.collector.Receiver: pid=29277 uid=10085 gids={50085, 3003, 1028, 1015}
12-19 22:58:29.174: D/Print(28831): resume()
答案 0 :(得分:8)
我发现这个 - Where is this toast coming from? 导致相机预览再次刷新。
ES文件资源管理器应用程序生成“内部存储空间左”吐司,我想暂停相机预览片刻,并且必须再次调用onResume()。禁用该Toast消息不再导致预览暂停和刷新。
感谢大家的帮助。 :)
答案 1 :(得分:2)
我遇到了同样的问题,可以通过NeilDA确认。
ES File Explorer
会中断我应用的活动生命周期!
它会导致onPause
迅速导致第二个onResume
。
(但我没看到任何吐司)
此仅在安装后首次打开或从构建后发生。之后,我可以多次重启应用程序,并且再次发生双重onResume
。
我在强制停止 ES文件浏览器之后检查了这个,重新构建了我的应用程序,并且双重onResume没有发生。然后重新打开ES文件资源管理器,再次重新构建,并且双onResume又回来了!
所以我卸载了ES File Explorer。