Я пытаюсь загрузить изображения из галереи и камеры в изображение с помощью Glider.
*** FIXED ***
Я не знаю, как обрабатывать URI. Я прикрепил URI, который я получаю из галереи и камеры
Камера: /my_images/Pictures/JPEG_20200806_213840_4160667046172070294.jpg
(я могу просматривать изображение в каталоге)
Галерея: /document/msf:24
fun openGallery(){
val takeimageg = registerForActivityResult(ActivityResultContracts.GetContent()){
it?.also {
ToastMessage("${it.path}")
recentPagerViewModel.setImage(it,"Gallery")
}
}
takeimageg.launch("image/*")
}
fun openCamera(){
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
ToastMessage("Error Creating File")
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
requireContext(),
"com.example.android.fileprovider",
it
)
takeimage = registerForActivityResult(ActivityResultContracts.TakePicture()){success ->
if(success){
ToastMessage("${photoURI.path}")
recentPagerViewModel.setImage(photoURI , "Camera")
}
}
takeimage.launch(photoURI)
}
}
@SuppressLint("SimpleDateFormat")
@Throws(IOException::class)
private fun createImageFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = context?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"JPEG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}
}
Glide.with(this)
.applyDefaultRequestOptions(
RequestOptions()
.placeholder(R.drawable.ic_launcher_background))
.load(it) // Chnaged to it from it.path
.into(imgview)