我正在编写一个使用相机的 Android 应用程序。
我拿了示例代码https://github.com/googlesamples/android-Camera2Basic/由谷歌提供。但是,我观察到调用 CameraDevice
的 close()
方法花费的时间太长(在我的三星 Galaxy S8 上几乎是一秒钟)。
此方法是从 onPause()
调用的,因此当相机 fragment 关闭时,应用程序会暂停一会儿。
@Override
public void onPause() {
closeCamera();
stopBackgroundThread();
super.onPause();
}
private void closeCamera() {
try {
mCameraOpenCloseLock.acquire();
if (null != mCaptureSession) {
mCaptureSession.close();
mCaptureSession = null;
}
if (null != mCameraDevice) {
mCameraDevice.close(); // This call takes 1 second!
mCameraDevice = null;
}
if (null != mImageReader) {
mImageReader.close();
mImageReader = null;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
mCameraOpenCloseLock.release();
}
}
当调用 closeCamera()
方法时,如何避免应用程序无响应?
我尝试从 UI 线程以外的另一个线程调用它,但在某些情况下应用程序会崩溃。
最佳答案
当我的设备 (mi a1 - oreo) 刷入补丁启动时,我遇到了这个问题。
如果您的设备也有补丁启动,请尝试使用原厂启动来刷新它。它对我的情况有所帮助。
关于android - 关闭 Android CameraDevice 很慢并挂起应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47621046/