In this Video we will capture image with Camera using the Camera Intent. Camera will be opened after clicking the button. If image is captured it will be set to an ImageView.
We will require following permission:
1) CAMERA
2) WRITE_EXTERNAL STORAGE
So we will also handle Runtime Permission for OS Marshmallow and above.
VIDEO
Step 1: Create a new Project or open new project
Step 2: Code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.blogspot.atifsoftwares.takepicture_kotlin"><!--Adding Camera , Write External Storage Permission--><uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"tools:context=".MainActivity"><!--ImageView in which image will be set--><ImageViewandroid:id="@+id/image_view"android:src="@drawable/ic_image_black_24dp"android:scaleType="centerCrop"android:layout_width="400dp"android:layout_height="400dp" /><!--Button to open camera--><Buttonandroid:id="@+id/capture_btn"android:text="Capture Image"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.takepicture_kotlinimport android.Manifestimport android.app.Activityimport android.content.ContentValuesimport android.content.Intentimport android.content.pm.PackageManagerimport android.net.Uriimport android.os.Buildimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.provider.MediaStoreimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {private val PERMISSION_CODE = 1000;private val IMAGE_CAPTURE_CODE = 1001var image_uri: Uri? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//button clickcapture_btn.setOnClickListener {//if system os is Marshmallow or Above , we need to request runtime permissionif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){if (checkSelfPermission(Manifest.permission.CAMERA)== PackageManager.PERMISSION_DENIED ||checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED){//permission was not enabledval permission = arrayOf(Manifest.permission.CAMERA , Manifest.permission.WRITE_EXTERNAL_STORAGE)//show popup to request permissionrequestPermissions(permission , PERMISSION_CODE)}else{//permission already grantedopenCamera()}}else{//system os is < marshmallowopenCamera()}}}private fun openCamera() {val values = ContentValues()values.put(MediaStore.Images.Media.TITLE , "New Picture")values.put(MediaStore.Images.Media.DESCRIPTION , "From the Camera")image_uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values)//camera intentval cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , image_uri)startActivityForResult(cameraIntent , IMAGE_CAPTURE_CODE)}override fun onRequestPermissionsResult(requestCode: Int , permissions: Array<out String> , grantResults: IntArray) {//called when user presses ALLOW or DENY from Permission Request Popupwhen(requestCode){PERMISSION_CODE -> {if (grantResults.size > 0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED){//permission from popup was grantedopenCamera()}else{//permission from popup was deniedToast.makeText(this , "Permission denied" , Toast.LENGTH_SHORT).show()}}}}override fun onActivityResult(requestCode: Int , resultCode: Int , data: Intent?) {//called when image was captured from camera intentif (resultCode == Activity.RESULT_OK){//set image captured to image viewimage_view.setImageURI(image_uri)}}}
0 Response to "Take Picture With Camera - Android Studio - Kotlin"