From cb3918124925caa29af067345bfde486b843056f Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Fri, 29 May 2020 15:55:48 +0200 Subject: [PATCH] Add button to take a photo This calls an external intent [1]. This does not work yet -- while the camera app is being called correctly, onActivityResult() always gets a -1 result. [1] https://developer.android.com/training/camera/photobasics --- app/src/main/AndroidManifest.xml | 3 ++ .../example/pittihelloworld/MainActivity.kt | 29 ++++++++++++++- app/src/main/res/layout/activity_main.xml | 37 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 99c7675..2407a60 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ + + + (R.id.messageText) - val message = editText.text.toString() + 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 diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b9c73fe..4fac27c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,4 +30,41 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/messageText" /> + +