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
<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"
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?) {
}
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
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
<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>