This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PermissionFragment : Fragment() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
if (!hasPermissions(requireContext())) { | |
requestPermissions( REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS ) | |
} else { | |
findNavController().popBackStack() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest ...> | |
<uses-feature android:name="android.hardware.camera.any" /> | |
<uses-permission android:name="android.permission.CAMERA" /> | |
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<application .../> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val videoSavedCallback = object : VideoCapture.OnVideoSavedCallback { | |
override fun onVideoSaved(file: File) { | |
val savedUri = Uri.fromFile(file) | |
FileUtils.scanFile(requireContext(), savedUri) | |
findNavController().navigate( CameraFragmentDirections.actionCameraFragmentToMediaViewerFragment( savedUri ) ) | |
} | |
override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) { | |
toast("Video capture failed: ${cause?.message}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val outputDirectory: File by lazy { FileUtils.getOutputDirectory(requireContext()) } | |
inner class ImageSavedCallback(private val photoFile: File) : | |
ImageCapture.OnImageSavedCallback { | |
override fun onImageSaved(output: ImageCapture.OutputFileResults) { | |
val savedUri = output.savedUri ?: Uri.fromFile(photoFile) | |
FileUtils.scanFile(requireContext(), savedUri) | |
findNavController().navigate( CameraFragmentDirections.actionCameraFragmentToMediaViewerFragment( savedUri ) ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun toggleCamera() { | |
cameraView.toggleCamera() | |
} | |
override fun toggleFlash(flashMode: ControlView.FlashMode) { | |
cameraView.flash = when (controlView.getFlashMode()) { | |
ControlView.FlashMode.FLASH_MODE_AUTO -> ImageCapture.FLASH_MODE_AUTO | |
ControlView.FlashMode.FLASH_MODE_ON -> ImageCapture.FLASH_MODE_ON | |
ControlView.FlashMode.FLASH_MODE_OFF -> ImageCapture.FLASH_MODE_OFF | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CameraFragment : Fragment(R.layout.fragment_camera), ControlView.Listener { | |
private lateinit var cameraExecutor: ExecutorService | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
cameraExecutor = Executors.newSingleThreadExecutor() | |
cameraView.bindToLifecycle(viewLifecycleOwner) | |
cameraView.captureMode = CameraView.CaptureMode.MIXED | |
controlView.setListener(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FileUtils { | |
fun getOutputDirectory(context: Context): File { | |
val appContext = context.applicationContext | |
val mediaDir = context.externalMediaDirs.firstOrNull()?.let { | |
File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() } | |
} | |
return if (mediaDir != null && mediaDir.exists()) | |
mediaDir else appContext.filesDir | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<androidx.constraintlayout.widget.ConstraintLayout ...> | |
<androidx.camera.view.CameraView | |
android:id="@+id/cameraView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:keepScreenOn="true" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
apply plugin: "androidx.navigation.safeargs" | |
//... | |
dependencies { | |
//... | |
//navigation | |
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun setupCaptureButtonListener() { | |
var initialTouchX = 0f | |
var initialTouchY = 0f | |
val mHandler = Handler() | |
val mLongPressed = Runnable { | |
isLongPressed = true | |
ivCapture.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) | |
timerView.startTimer() | |
listener?.startVideoCapturing() | |
} |