背景:今天早上做了一个拍照后就上传到后台的需求,这个需求还是蛮好处理的,不就是拍照嘛,信誓旦旦说等会就打包出来,结果引发了思考,搞到了下午才打包。
拍照
/**
* 拍照
*/
private fun toTakePicture() {
currentTakePhotoTime = System.currentTimeMillis()
val takeIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val file = getTakePicktureFile()
Log.i(TAG, file.path)
val uri = getUriForFile(file)
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(takeIntent, REQUESTCODE_FILECHOOSER)
}
/**
* 指定调用相机拍照后的照片存储的路径
*/
private fun getTakePicktureFile(): File {
val imageJpg = "webimage_$currentTakePhotoTime.jpg"
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File(getPhotoParent(), imageJpg)
} else {
File(
getExternalFilesDir(Environment.DIRECTORY_PICTURES),
imageJpg
)
}
}
//根据file获取Uri
private fun getUriForFile(file: File