Click here to Skip to main content
15,745,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently working on an Android app that involves camera object detection. However, I have encountered some issues that need to be addressed:
- The app displays a black screen after granting camera permission, requiring one or two app restarts to open the camera successfully.
I also made an alert dialog to prompt the user if camera permission is not granted. However, the alert dialog has some bugs:
- If the user clicks "OK" in the alert dialog and then denies camera permission, the app enters into a loop and keeps displaying the alert dialog repeatedly.
- If the user denies camera permission initially, and then grants it by clicking "OK" in the alert dialog, the app closes unexpectedly.

What I have tried:

This is the permission functions:
    fun getPermission() {
        if (ContextCompat.checkSelfPermission(
            ) != PackageManager.PERMISSION_GRANTED
            requestPermissions(arrayOf(android.Manifest.permission.CAMERA), 101)

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray)
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 101 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            val alert = PermissionDialog()
  , null)

This is the alert dialog:
import android.os.Bundle
import android.widget.Toast
import com.example.sightfulkotlin.MainActivity
import com.example.sightfulkotlin.R

class PermissionDialog : DialogFragment() {

    private lateinit var mediaPlayer: MediaPlayer

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(activity)
            .setTitle("Camera Permission")
            .setMessage("Camera permission denied, our app needs camera access to detect obstacles ahead. We value your privacy and use the camera solely for this purpose. Please grant permission to enjoy our app's features, Thank you!")
            .setPositiveButton("Ok") { _, _ -> (activity as MainActivity).getPermission()}
            .setNegativeButton("Cancel") { _, _ ->
                Toast.makeText(context, "Sorry, we can't make the app work without the camera permission.", Toast.LENGTH_LONG).show()
        return builder.create()
Gerry Schmitz 14-Apr-23 9:36am    
Without knowing Kotlin, I think I can say there is not enough "procedural logic" in the code you're showing that would give anyone else a clue to your problem.
Fatema Shawki 14-Apr-23 10:11am    
Is there anything else you require?
Gerry Schmitz 19-Apr-23 15:09pm    
Just a tip: "dialogs" don't "perform"; they simply return "yes" or "no" (and maybe a bit else). The "main" program then perform (get permission or whatever). That will at least get you out of your dialog trap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900