AndroidManifest.xml
を変更する必要はない。
MainActivity.kt
package com.sample.android.camerasample
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
private val _requestCodeFood = 1000
private val _requestCodeDrink = 2000
private var _imageUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == RESULT_OK) {
val ivCamera = findViewById<ImageView>(R.id.ivCamera)
ivCamera.setImageURI(_imageUri)
if (requestCode == _requestCodeFood) {
Toast.makeText(applicationContext, "Food Saved", Toast.LENGTH_SHORT).show()
} else if (requestCode == _requestCodeDrink) {
Toast.makeText(applicationContext, "Drink Saved", Toast.LENGTH_SHORT).show()
}
}
}
fun onFoodButtonClick(view: View) {
_imageUri = makeImageUri("Food")
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri)
startActivityForResult(intent, _requestCodeFood)
}
fun onDrinkButtonClick(view: View) {
_imageUri = makeImageUri("Drink")
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri)
startActivityForResult(intent, _requestCodeDrink)
}
private fun makeFileName(head: String): String {
val dateFormat = SimpleDateFormat("yyyyMMddHmmss")
val nowStr = dateFormat.format(Date())
val fileName = "${head}_${nowStr}.jpg"
return fileName
}
private fun makeImageUri(head: String): Uri {
val fileName = makeFileName(head)
val resolver = applicationContext.contentResolver
val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
put(MediaStore.Images.Media.TITLE, fileName)
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
}
val imageUri = resolver.insert(collection, values)!!
return imageUri
}
}
strings.xml
<resources>
<string name="app_name">CameraSample</string>
<string name="food">食事</string>
<string name="drink">飲み物</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivCamera"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/llButtons"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center"/>
<LinearLayout
android:id="@+id/llButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnEat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/food"
android:textSize="22sp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:onClick="onFoodButtonClick"/>
<Button
android:id="@+id/btnBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/drink"
android:textSize="22sp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:onClick="onDrinkButtonClick"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
コメント