I was trying to use the native camera app that comes pre-installed in Android devices to capture the images for my app. For this I've been using this code:
A function is called when the user taps on button
take_img_btn.setOnClickListener {
dispatchTakePictureIntent()
}
That function is supposed to do this:
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
takePictureIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
ex.printStackTrace()
null
}
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
}
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yy-MM-dd-HH-mm-ss-SS",Locale.getDefault()).format(System.currentTimeMillis())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"FNL_${timeStamp}_",
".jpg",
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
currentPhotoName = name
}
}
And the FileProvider is configured in the Manifest like so
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
...
</application>
I am also asking for Camera feature in Manifest
<manifest>
...
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
When I run this app on my android device (Realme 6 Pro) and tap the button then:
- The app opens the native camera app
- I can capture the image
- The image is saved to the location where it was specified
But when this same app is deployed on OnePlus devices (in our case OnePlus Nord) and tap the button then nothing happens. It feels like the button is not programmed.
This code is taken from here: https://developer.android.com/training/camera/photobasics
It would be really helpful if someone help.