Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. private fun startCameraIntent() {
  2.  
  3. Chat.stopCameraX()
  4.  
  5. Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
  6. takePictureIntent.resolveActivity(requireContext().packageManager)?.also {
  7.  
  8. val photoFile: File? = try {
  9. createImageFile()
  10. } catch (exception: IOException) {
  11. logger.e("Failed to get image file: ${exception.message}")
  12. displayErrorMessage(getString(R.string.cant_create_photo))
  13. null
  14. }
  15.  
  16. photoFile?.also { file ->
  17. val photoURI: Uri = FileProvider.getUriForFile(
  18. requireContext(),
  19. requireContext().applicationContext.packageName + ".provider",
  20. file
  21. )
  22.  
  23. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
  24. startActivityForResult(takePictureIntent, CAMERA_ACTIVITY_FOR_RESULT)
  25. }
  26. }
  27. }
  28. }
  29.  
  30. private fun createImageFile(): File? {
  31. val timeStamp =
  32. SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date())
  33.  
  34. val storageDir = context?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
  35.  
  36. return File.createTempFile(
  37. "JPEG_${timeStamp}_",
  38. ".jpg",
  39. storageDir
  40. ).also {
  41. cameraImageUri = Uri.fromFile(it)
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement