重新打开摄像头后意图选择器被取消 [英] Reopening camera after intent chooser is canceled

查看:241
本文介绍了重新打开摄像头后意图选择器被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的摄像头preVIEW视图 CameraView 延伸 SurfaceView ,而且还实现了 SurfaceHolder.Callback 接口。视图操作与相机。当您打开视图它显示了一个摄像头preVIEW。在同一屏幕上也有覆盖有两个按钮 - 拍照,选择从画廊。即持有 CameraView 版本并重新打开相机的onPause() onResume(活动)的方法。

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.

如果我点击选择从图库按钮,出现如下意图创建的:

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);

如果只有一个可以此意图作出反应活性,然后它的罚款。该活动被自动打开,摄像机被释放。我还可以打回来时在走廊上,我将重新进入 CameraView 的活动,而且摄像头preVIEW恢复。

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.

有趣的部分开始,如果有多个活动,可以处理这个意图,而故意选择器对话框弹出。当意图选择对话框产卵,的onPause()被称为父活动和摄像头被释放,屏幕变黑。如果我不选择对话的意图,而是单击后退按钮电话 onResume()的呼吁,但摄像头preVIEW是永远不会回来了。让相机preVIEW再次表明,我需要回去previous活动并回到了preVIEW活动中。

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()被调用,但如果我真的切换到不同的活动 surfaceDestroyed( )被调用也。为 onResume()时,取消了与后退按钮的对话框中, surfaceChanged()和<$ C也是如此$ C> surfaceCreated()永远不会被调用。

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.

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

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.

推荐答案

,你在OnStart方法打开你的摄像头,并在活动的onStop方法释放它是正确的。该方法onResume和的onPause是机器人活动的可见生命周期的一部分。当另一活动占据了整个可见空间的onStop被调用。将OnPause甚至被称为在另一项活动来到前台,即使它不占用整个可见的空间,如意图选择对话框不会弹出时。所以我想将您的相机创作和发布到正确的生命周期方法应该做的伎俩。 你可以找到关于此活动的生命周期的详细信息,但我敢肯定,你所熟悉的是:

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:

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

这篇关于重新打开摄像头后意图选择器被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆