fun startCamera(context: Activity) {
/**** 方法1 *****/
// val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// PATH_PHOTO = getSdCardDirectory(context) + "/temp.png"
// val temp = File(PATH_PHOTO)
// if (!temp.parentFile.exists()) {
// temp.parentFile.mkdirs()
// }
// if (temp.exists()) {
// temp.delete()
// }
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// //添加这一句表示对目标应用临时授权该Uri所代表的文件
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
// // 通过FileProvider创建一个content类型的Uri
// val uri: Uri = FileProvider.getUriForFile(context, context.packageName + ".fileprovider", temp)
// intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
// } else {
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp))
// }
// context.startActivityForResult(intent, RESULT_CODE_CAMERA)
/***** 方法二 不用fileProvider ****/
val mImageCaptureUri: Uri
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val externalDataDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
val cameraDataDir = File(externalDataDir.absolutePath
+ File.separator +"browser-photo")
cameraDataDir.mkdirs()
PATH_PHOTO = (cameraDataDir.absolutePath + File.separator
+ System.currentTimeMillis() +".jpg")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val contentValues = ContentValues(1)
contentValues.put(MediaStore.Images.Media.DATA, File(PATH_PHOTO).getAbsolutePath())
mImageCaptureUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mImageCaptureUri)
}else {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(File(PATH_PHOTO)))
}
context.startActivityForResult(cameraIntent, RESULT_CODE_CAMERA)
}
网友评论