Add button to take a photo master
authorMartin Pitt <martin@piware.de>
Fri, 29 May 2020 13:55:48 +0000 (15:55 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 29 May 2020 13:55:48 +0000 (15:55 +0200)
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
app/src/main/java/com/example/pittihelloworld/MainActivity.kt
app/src/main/res/layout/activity_main.xml
app/src/main/res/values/strings.xml

index 99c7675..2407a60 100644 (file)
@@ -2,6 +2,9 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.pittihelloworld">
 
+    <uses-feature android:name="android.hardware.camera" android:required="true" />
+    <!-- <uses-permission android:name="android.permission.CAMERA" /> does not help -->
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
index 540fc06..a75e653 100644 (file)
@@ -1,12 +1,18 @@
 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?) {
@@ -15,11 +21,30 @@ class MainActivity : AppCompatActivity() {
     }
 
     fun sendMessage(view: View) {
-        val editText = findViewById<EditText>(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
index b9c73fe..4fac27c 100644 (file)
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.5"
         app:layout_constraintStart_toEndOf="@+id/messageText" />
+
+    <Button
+        android:id="@+id/button_photo"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="16dp"
+        android:layout_marginLeft="16dp"
+        android:layout_marginTop="16dp"
+        android:onClick="takePhoto"
+        android:text="@string/button_photo"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/messageText" />
+
+    <ImageView
+        android:id="@+id/imageViewPhoto"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:layout_marginStart="16dp"
+        android:layout_marginLeft="16dp"
+        android:layout_marginTop="16dp"
+        android:layout_marginEnd="16dp"
+        android:layout_marginRight="16dp"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/button_photo"
+        app:srcCompat="@android:drawable/btn_dialog" />
+
+    <TextView
+        android:id="@+id/textViewPhotoResult"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="16dp"
+        android:layout_marginLeft="16dp"
+        android:layout_marginTop="16dp"
+        android:text="@string/text_nocamresult"
+        app:layout_constraintStart_toEndOf="@+id/button_photo"
+        app:layout_constraintTop_toBottomOf="@+id/messageText" />
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
index 5f138a8..5631ead 100644 (file)
@@ -2,4 +2,6 @@
     <string name="app_name">"Pitti's Hello World"</string>
     <string name="edit_message">Enter a message</string>
     <string name="button_send">Send</string>
+    <string name="button_photo">Take a photo</string>
+    <string name="text_nocamresult">No photo taken yet</string>
 </resources>