So i have a function to open camera and gallery and i have an image view to set that image and save it in room database. When i am selecting gallery everything works fine image is displayed in image view and also saved in room database but when i capture image from camera intent received in start activity result is null. hence data.data is also null
private fun imageChooser (activity: Activity) {
val i = Intent ()
i.type = "image/*"
i.action = Intent.ACTION_GET_CONTENT
activity.startActivityForResult(i, 0)
}
private fun displayCamera(activity: Activity) :Uri {
lateinit var imagePath : Uri
val destPath: String? = Objects.requireNonNull(
Objects.requireNonNull(activity).getExternalFilesDir(null)!!
).absolutePath
val imagesFolder = File(destPath, activity.resources.getString(R.string.app_name))
try {
imagesFolder.mkdirs()
val imgFile = File(imagesFolder, Date().time.toString() + ".jpg")
imagePath = FileProvider.getUriForFile(
activity, BuildConfig.APPLICATION_ID + ".provider", imgFile
)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePath)
activity.startActivityForResult(intent, 1)
} catch (e: Exception) {
e.printStackTrace()
}
return imagePath
}
now code for start activity result
@Deprecated ("Deprecated in Java")
override fun onActivityResult (requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (requestCode == 0) {
val selectedImageUri = data?.data
if (selectedImageUri != null) {
databinding.profileImage.setImageURI(selectedImageUri)
path = getPath(this, selectedImageUri).toString()
}
}
if (requestCode == 1) {
this.imagePath = Uri.parse(intent.getStringExtra(EXTRA_OUTPUT))
path = data?.data.toString()
databinding.profileImage.setImageURI(imagePath)
}
}
}
also here is the screenshot of debug for camera enter image description here
You can do this by kotlin like this. You need to check the permission for camera and file storage. On Click of any button you can open camera and intent and capture image before capture you will need to create path for that image. like below:
fun onIntentCamera(startActivityIntent: ActivityResultLauncher<Intent>) {
if (allPermissionsGranted()) {
try {
val chooserIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
captureFile = createImageFile(this@TIDProcessActivity)
val photoURI = FileProvider.getUriForFile(
this@TIDProcessActivity,
"com.example.fileprovider",
captureFile
)
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityIntent.launch(chooserIntent)
} catch (e: Exception) {
Log.e("TAG", "onCreate: " + e.message)
}
} else {
requestPermissions(
REQUIRED_PERMISSIONS,
REQUEST_CODE_PERMISSIONS
)
}
for the result of image you can do like this:
var startActivityIntentDbPath =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (this::captureFile.isInitialized) {
val bitmap = BitmapFactory.decodeFile(captureFile.absolutePath)
if (bitmap != null) {
val fileName = System.currentTimeMillis().toString()
saveImageToDirectory(bitmap, fileName)
binding.selectedDbImagePath.visibility = View.VISIBLE
Glide.with(this).load(bitmap).into(binding.selectedDbImagePath)
}
}
}