private fun showAlertView() {
AlertView("选择图片", "", "取消", null, arrayOf("拍照", "相册"), this,
AlertView.Style.ActionSheet, object : OnItemClickListener {
override fun onItemClick(o: Any?, position: Int) {
mTakePhoto.onEnableCompress(CompressConfig.ofDefaultConfig(), false)
when (position) {
0 -> {
createTempFile()
checkCameraPermission()
}
1 -> mTakePhoto.onPickFromGallery()
}
}
}).show()
}
fun checkCameraPermission() {
if (Build.VERSION.SDK_INT >= 23) {
val checkCallPhonePermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
1// 1:OPEN_CANMER
)
return
} else {
capture()
}
} else {
capture()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
{
AskForPermission()
return
}
when (requestCode) {
1 -> {//OPEN_CANMER
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
capture()
} else {
toast("相机权限禁用了。请务必开启相机权")
}
}
else ->
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private fun AskForPermission() {
var builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle("需要摄像头权限")
builder.setNegativeButton("取消", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, which: Int) {
}
})
builder.setPositiveButton("Settings", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, which: Int) {
var intent: Intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:${getPackageName()}")) // 根据包名打开对应的设置界面
startActivity(intent)
}
})
builder.create().show()
}
fun capture() {
mTakePhoto.onPickFromCapture(Uri.fromFile(mTempFile))
}