嘿,今天的你过的还好吗,今天分享kotlin获取系统相机拍照返回图片
这个需求源自于项目中,拍照获取缩略图并显示出来
只是具体也没有什么
1.intent跳转相机
2.拍照获取缩略图
3.显示出来
var CAMERA_CODE = 0
lateinit var picture_scene_img: ImageView
private fun takePhoto() {
val state: String = Environment.getExternalStorageState() //拿到sdcard是否可用的状态码
if (state == Environment.MEDIA_MOUNTED) { //如果可用
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_CODE)
} else {
Toast.makeText(activity, "sdcard不可用", Toast.LENGTH_SHORT).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == CAMERA_CODE && resultCode== RESULT_OK){
var bitmap: Bitmap = data?.extras?.get("data") as Bitmap
picture_scene_img.setImageBitmap(bitmap)
}
}
跟java代码其实差不多,转变一下思想就行了
网友评论