意图选择器取消后重新打开相机

时间:2022-06-08 15:30:20

I have created a custom camera preview view CameraView which extends SurfaceView, and it also implements SurfaceHolder.Callback interface. The view operates with the camera. When you open the view it shows a camera preview. On the same screen there is also overlay with two buttons - 'Take picture', 'Choose from gallery'. The activity that holds the CameraView releases and reopens the camera in onPause() and onResume() methods.

我创建了一个自定义相机预览视图CameraView,它扩展了SurfaceView,它还实现了SurfaceHolder.Callback接口。视图使用相机操作。当您打开视图时,它会显示相机预览。在同一个屏幕上还有两个按钮叠加 - “拍照”,“从图库中选择”。持有CameraView的活动在onPause()和onResume()方法中释放并重新打开相机。

If I click the 'Choose from gallery' button, the following intent is created:

如果单击“从库中选择”按钮,将创建以下意图:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, LOAD_PICTURE);

If there is only one activity that can respond to this intent then it's fine. The activity gets automatically opened, the camera is released. I can also hit back when on the gallery and I will get back into the CameraView activity, and camera preview is restored.

如果只有一个活动可以响应这个意图,那就没关系。活动自动打开,相机被释放。我也可以在画廊上回击,然后我将重新进入CameraView活动,并恢复相机预览。

The interesting part starts if there are multiple activities that can handle this intent, and the intent chooser dialog pops up. When intent chooser dialog spawns, onPause() gets called in parent activity and camera gets released, the screen becomes black. If I dont choose intent from the dialog, but instead click back button on the phone onResume() is called, but camera preview is never coming back. To get the camera preview to show again, I need to go back to previous activity and go back inside the preview activity.

如果有多个活动可以处理此意图,则会启动有趣的部分,并弹出意图选择器对话框。当意图选择器对话框产生时,在父活动中调用onPause()并释放相机,屏幕变为黑色。如果我不从对话框中选择意图,而是单击手机上的后退按钮onResume()被调用,但相机预览永远不会回来。要让相机预览再次显示,我需要返回上一个活动并返回预览活动。

The following problem happens because, when the dialog is raised only onPause() gets called, but if I actually switch to different activity surfaceDestroyed() get called also. The same is true for onResume() when the dialog is canceled with back button, surfaceChanged() and surfaceCreated() never gets called.

出现以下问题是因为,当对话框被引发时,只有onPause()被调用,但是如果我实际切换到不同的活动,则表明也会调用faceDestroyed()。当使用后退按钮取消对话框时,onResume()也是如此,不会调用surfaceChanged()和surfaceCreated()。

My question is how to get camera preview to reappear if the intent chooser dialog is canceled. Is there a way how to trigger SurfaceHolder.Callback methods explicitly? I know that there are hidden hideSurface() and showSurface() in SurfaceView, but I don't want to go this route.

我的问题是如果意图选择器对话框被取消,如何让相机预览重新出现。有没有办法明确触发SurfaceHolder.Callback方法?我知道SurfaceView中有隐藏的hideSurface()和showSurface(),但我不想走这条路。

1 个解决方案

#1


0  

It is right, you have open your camera in the onStart method and release it in the onStop method of your activity. The methods onResume and onPause are part of the visible lifecycle of the android activity. OnStop is called when another activity occupies the whole visible space. OnPause is even called when another activity comes to the foreground even if it does not occupy the whole visible space such as the intent chooser dialog does when it pops up. So I guess moving your camera creation and release into the correct lifecycle methods should do the trick. You can find further information about the activity lifecycle here, but I'm sure you are familiar with that:

是的,您已在onStart方法中打开相机并在活动的onStop方法中释放它。 onResume和onPause方法是android活动的可见生命周期的一部分。当另一个活动占据整个可见空间时,将调用OnStop。当另一个活动进入前台时,即使它没有占据整个可见空间,例如意图选择器对话框弹出时,也会调用OnPause。因此,我认为将相机创建和发布移动到正确的生命周期方法应该可以解决问题。您可以在此处找到有关活动生命周期的更多信息,但我相信您对此很熟悉:

http://developer.android.com/reference/android/app/Activity.html

http://developer.android.com/reference/android/app/Activity.html

#1


0  

It is right, you have open your camera in the onStart method and release it in the onStop method of your activity. The methods onResume and onPause are part of the visible lifecycle of the android activity. OnStop is called when another activity occupies the whole visible space. OnPause is even called when another activity comes to the foreground even if it does not occupy the whole visible space such as the intent chooser dialog does when it pops up. So I guess moving your camera creation and release into the correct lifecycle methods should do the trick. You can find further information about the activity lifecycle here, but I'm sure you are familiar with that:

是的,您已在onStart方法中打开相机并在活动的onStop方法中释放它。 onResume和onPause方法是android活动的可见生命周期的一部分。当另一个活动占据整个可见空间时,将调用OnStop。当另一个活动进入前台时,即使它没有占据整个可见空间,例如意图选择器对话框弹出时,也会调用OnPause。因此,我认为将相机创建和发布移动到正确的生命周期方法应该可以解决问题。您可以在此处找到有关活动生命周期的更多信息,但我相信您对此很熟悉:

http://developer.android.com/reference/android/app/Activity.html

http://developer.android.com/reference/android/app/Activity.html