Add button to take a photo
[android-PittiHelloWorld.git] / app / src / main / java / com / example / pittihelloworld / MainActivity.kt
index 9e808dca4e24c92a2b8288941dc3e6a42a3303a8..a75e6534c8da2e3edade651195ebc76072d2a7e6 100644 (file)
@@ -1,11 +1,50 @@
 package com.example.pittihelloworld
 
+import android.app.Activity
+import android.content.Intent
+import android.graphics.Bitmap
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
+import android.provider.MediaStore
+import android.view.View
+import android.widget.EditText
+import android.widget.TextView
+import kotlinx.android.synthetic.main.activity_main.*
+
+const val EXTRA_MESSAGE = "com.example.pittihelloworld.MESSAGE"
+const val REQUEST_IMAGE_CAPTURE = 1
 
 class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
     }
+
+    fun sendMessage(view: View) {
+        val message = messageText.text.toString()
+        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
+            putExtra(EXTRA_MESSAGE, message)
+        }
+        startActivity(intent)
+    }
+
+    fun takePhoto(view: View) {
+        Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
+            takePictureIntent.resolveActivity(packageManager)?.also {
+                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
+            }
+        }
+    }
+
+    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+        super.onActivityResult(requestCode, resultCode, data)
+        textViewPhotoResult.text = "result code: " + resultCode.toString()
+        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK && data != null) {
+            val extras = data.extras
+            if (extras != null) {
+                val imageBitmap = extras.get("data") as Bitmap
+                imageViewPhoto.setImageBitmap(imageBitmap)
+            }
+        }
+    }
 }
\ No newline at end of file