]> piware.de Git - android-PittiHelloWorld.git/commitdiff
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 99c7675cc86954d7a0a5eaab68fae0ed3a8be570..2407a605f7b07c4846cd4433200853aee1dd51e2 100644 (file)
@@ -2,6 +2,9 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.pittihelloworld">
 
 <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"
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
index 540fc060300fb7a46240354ac5c0babb712cdd06..a75e6534c8da2e3edade651195ebc76072d2a7e6 100644 (file)
@@ -1,12 +1,18 @@
 package com.example.pittihelloworld
 
 package com.example.pittihelloworld
 
+import android.app.Activity
 import android.content.Intent
 import android.content.Intent
+import android.graphics.Bitmap
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
+import android.provider.MediaStore
 import android.view.View
 import android.widget.EditText
 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 EXTRA_MESSAGE = "com.example.pittihelloworld.MESSAGE"
+const val REQUEST_IMAGE_CAPTURE = 1
 
 class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
 
 class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -15,11 +21,30 @@ class MainActivity : AppCompatActivity() {
     }
 
     fun sendMessage(view: View) {
     }
 
     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)
     }
         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
 }
\ No newline at end of file
index b9c73fee784840912d7f398888fb69b5df3dc4c3..4fac27c5b052c71e01b164029c54bc8ade7b85da 100644 (file)
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.5"
         app:layout_constraintStart_toEndOf="@+id/messageText" />
         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
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
index 5f138a8ed70f087bde36f3e77088be8bd9bc435b..5631ead613485210ad24d00cdefac49afd774e56 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="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>
 </resources>