Capture and save image with Kotlin in Android Studio

16,377

Solution 1

I think maybe the next code can help you half way. Just capture the image from camera and display it in an ImageView.

I used it (found in Creating camera intent app using Kotlin on android - Displaying thumbnail).

val CAMERA_REQUEST_CODE = 0
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    button.setOnClickListener {
        val callCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (callCameraIntent.resolveActivity(packageManager) != null) {
            startActivityForResult(callCameraIntent, CAMERA_REQUEST_CODE)
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when(requestCode) {
        CAMERA_REQUEST_CODE -> {
            if (resultCode == Activity.RESULT_OK && data != null) {
                imageView.setImageBitmap(data.extras.get("data") as Bitmap)
            }
        }
        else -> {
            Toast.makeText(this, "Unrecognized request code", Toast.LENGTH_SHORT)
        }
    }
}

Solution 2

The simplest code is to, open the native camera app to take a picture and handle result in the OnActivityResult method, as shown in the article Capture Picture with Camera in Kotlin – Android.

val REQUEST_CODE = 200

fun capturePhoto() {

    val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    startActivityForResult(cameraIntent, REQUEST_CODE)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE && data != null){
        imageView.setImageBitmap(data.extras.get("data") as Bitmap)
    }
}

That's it.

Share:
16,377

Related videos on Youtube

Steven Colocho
Author by

Steven Colocho

Updated on June 04, 2022

Comments

  • Steven Colocho
    Steven Colocho almost 2 years

    I need help with Kotlin. I need to capture and save an image in my media store.

    My code:

    class MainActivity : AppCompatActivity() {
        var ListadeProductos = ArrayList<Notas>()
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            camera.setOnClickListener {
    
                val intentCamera = Intent("android.media.action.IMAGE_CAPTURE")
                startActivity(intentCamera)
            }
        }
    }
    
    • dominicoder
      dominicoder over 6 years
      And your question is ... ? stackoverflow.com/help/how-to-ask
    • Zoe stands with Ukraine
      Zoe stands with Ukraine over 6 years
      This isn't an Android Studio issue, so don't use the Android Studio tag
  • DarkCygnus
    DarkCygnus over 6 years
    You "saw" it? Did you tested it before posting or was this just a guess?...