From: Martin Pitt Date: Fri, 29 May 2020 13:55:48 +0000 (+0200) Subject: Add button to take a photo X-Git-Url: https://piware.de/gitweb/?a=commitdiff_plain;ds=inline;p=android-PittiHelloWorld.git 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 --- 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" /> + +