我试图使用 Android 设备中预装的本机相机应用程序来为我的应用程序捕获图像。为此,我一直在使用这段代码:
当用户点击按钮时调用一个函数
take_img_btn.setOnClickListener {
dispatchTakePictureIntent()
}
Run Code Online (Sandbox Code Playgroud)
该函数应该执行以下操作:
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 …Run Code Online (Sandbox Code Playgroud)