eto123
eto123

Reputation: 83

Android studio kotlin open camera when ImageButton clicked dont work

I need to open camera app when ImageButton is clicked. Here is my code to do this:

var btnAddPhoto = findViewById<ImageButton>(R.id.addPhotoButton)
        btnAddPhoto.setOnClickListener {
            val intent = Intent("MediaStore.ACTION_IMAGE_CAPTURE")
            startActivityForResult(intent, 1)
        }

But when I run it my activity is changed to previous one. Can you see what is wrong here?

Upvotes: 2

Views: 2011

Answers (1)

The_ehT
The_ehT

Reputation: 1230

Your intent declaration is wrong, there should be no quotation around MediaStore.ACTION_IMAGE_CAPTURE. Correct initialization looks like following -

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

For this you also need to import the package given below

import android.provider.MediaStore

Upvotes: 1

Related Questions