我正在尝试在一个针对 Android Q 的项目中实现从相机拍照。根据我的发现,我已经实现了这个:
val mediaFile = mediaManager.createImageFile() // creates a file to store the image and returns Uri
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.apply {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
type = MIME_TYPE_IMAGE //"image/*"
intent.putExtra(MediaStore.EXTRA_OUTPUT, mediaFile)
}
startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO)
创建图像文件:
override fun createImageFile(): Uri? {
return createFile(mimeType = MIME_IMAGE_JPG, isImage = true)
}
private fun createFile(mimeType: String, isImage: Boolean): Uri? {
val mediaFileName = createTempName()
val path = if (isImage) {
Environment.DIRECTORY_PICTURES
} else {
Environment.DIRECTORY_MOVIES
}
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, mediaFileName)
put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
put(MediaStore.MediaColumns.RELATIVE_PATH, path)
}
val resolver = context.contentResolver
val contentUri = if (isImage) {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
} else {
MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
return resolver.insert(contentUri, contentValues)
}
但是,我得到android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE typ=image/* flg=0x3 clip={text/uri-list U:content://media/external/images/media/109855} (has extras) }
我搜索了几个小时并按照说明做了所有事情,但仍然没有运气。有趣的是,三星 Galaxy S8createImageFile()
回归了null
(天哪,我讨厌三星,这对那些人来说总是一些特殊情况)。我在这里做错了什么?