Код: Выделить всё
@Throws(IOException::class)
private fun createImageFile(): File {
val timeStamp: String =
SimpleDateFormat(
"yyyyMMdd_HHmmss",
Locale.getDefault()
).format(Date())
val imageFileName = "IMG_" + timeStamp + "_"
val storageDir =
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
stored.imageFilePath = image.absolutePath
return image
}
private fun takeAPicture() {
var photoFile: File? = null
try {
photoFile = createImageFile();
} catch (ex: IOException) {
// Error occurred while creating the File
}
if (photoFile != null) {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val author = "${packageName}.provider"
try {
val photoURI: Uri = FileProvider.getUriForFile(this, author, photoFile);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT,
photoURI
)
cameraIntent.putExtra("saveToGallery", false)
activityTakePictreLauncher.launch(cameraIntent)
} catch (ex: Exception) {
Log.e("MainActivity", ex.message.toString())
}
}
}
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/786 ... -to-galler
Мобильная версия