在Kotlin中设置渗透

时间:2019-03-29 08:53:54

标签: android kotlin camera android-permissions

当我单击适配器时,希望它显示一个包含3个项目的对话框:take photoselect imagecancel。 如果他们选择“拍照”,则会弹出有关权限的对话框。当用户单击allow按钮时,它将立即采取相机操作。

mImageListAdapter.mAddImageClickListener = object : ImageListAdapter.AddImageClickListener {
            override fun addImageClicked() {
                val options = arrayOf<CharSequence>("Take Photo", "Choose From Gallery", "Cancel")
                val builder = android.support.v7.app.AlertDialog.Builder(context!!)
                builder.setTitle("Select Option")
                builder.setItems(options) { dialog, item ->
                    if (options[item] == "Take Photo") {
                        if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                            startActivityForResult(intent, CAMERA_CAPTURE)
                            dialog.dismiss()
                        }
                    } else if (options[item] == "Choose From Gallery") {
                        dialog.dismiss()
                        val pickPhoto = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
                        startActivityForResult(pickPhoto, REQUEST_IMAGE_GALLERY)
                    } else if (options[item] == "Cancel") {
                        dialog.dismiss()
                    }
                }
                builder.show()
            }
        }

我现在的问题是,当我单击适配器时,将弹出对话框。当我选择take photo时,将弹出一个权限对话框。但是,当我单击权限对话框中的allow按钮时,它不会采取相机操作,而是仅关闭权限对话框。

我应该在哪里放置requestPermisson函数?

RequestPermission

 fun requestPermission(permission: String): Boolean {
        val isGranted = ContextCompat.checkSelfPermission(context!!, permission) == PackageManager.PERMISSION_GRANTED
        if (!isGranted) {
            ActivityCompat.requestPermissions(
                activity,
                arrayOf(permission),
                READ_WRITE_STORAGE
            )
        }
        return isGranted
    }

2 个答案:

答案 0 :(得分:0)

在授予许可后,您可以打开画廊或照相机,无论您想要什么,如果没有获得许可,我们都必须再次请求许可

val selectTitle = ""
mImageListAdapter.mAddImageClickListener = object : ImageListAdapter.AddImageClickListener {
    override fun addImageClicked() {
        val options = arrayOf<CharSequence>("Take Photo", "Choose From Gallery", "Cancel")
        val builder = android.support.v7.app.AlertDialog.Builder(context!!)
        builder.setTitle("Select Option")
        builder.setItems(options) { dialog, item ->
            selectTitle = options[item]
            if (options[item] == "Take Photo") {
                dialog.dismiss()
                if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                    startActivityForResult(intent, CAMERA_CAPTURE)
                }
            } else if (options[item] == "Choose From Gallery") {
                dialog.dismiss()
                if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    val pickPhoto = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
                    startActivityForResult(pickPhoto, REQUEST_IMAGE_GALLERY)
                }
            } else if (options[item] == "Cancel") {
                dialog.dismiss()
            }
        }
        builder.show()
    }
}


fun requestPermission(title: String,permission: String): Boolean {
    val isGranted = ContextCompat.checkSelfPermission(context!!, permission) == PackageManager.PERMISSION_GRANTED
    if (!isGranted) {
        ActivityCompat.requestPermissions(
                activity,
                arrayOf(permission),
                READ_WRITE_STORAGE
        )
    }
    return isGranted
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == READ_WRITE_STORAGE) {
        if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //permission granted so open camera or gallery based on you click
            if(selectTitle == "Take Photo")
                ActivityCompat.startActivityForResult(Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_CAPTURE)
            else if(selectTitle =="Choose From Gallery")
                ActivityCompat.startActivityForResult(Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), REQUEST_IMAGE_GALLERY)
        } else {
            // permission was not granted user choose never option from alert dialog of permission
            if (!ActivityCompat.shouldShowRequestPermissionRationale(context as Activity, permissions[0]))
                showPermissionDialog(context)
        }
    }
}
  

当用户从权限对话框中选择从不选项时,这将打开设备设置屏幕

fun showPermissionDialog(mContext: Context) {
            val builder = AlertDialog.Builder(mContext, R.style.Dialog)
            builder.setTitle("Need Permission")
            builder.setMessage(msg)
            builder.setPositiveButton("YES") { dialogInterface, i ->
                dialogInterface.dismiss()
                val intent = Intent()
                intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                val uri = Uri.fromParts("package", mContext.packageName, null)
                intent.data = uri
                mContext.startActivity(intent)
            }

            builder.setNegativeButton("NO") { dialogInterface, i -> dialogInterface.dismiss() }
            builder.show()
        }

答案 1 :(得分:0)

也许您应该实现onRequestPermissionsResult方法。

  override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

    //TODO

    super.onRequestPermissionsResult(requestCode, permissions, grantResults)

}