我正试图在一个针对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) }
我找了几个小时,按规定做了所有事情,但仍然没有运气。
有趣的是,
createImageFile()
退货
null
三星Galaxy S8(天哪,我讨厌三星,总是有一些特殊情况)。
我在这里做错了什么?