Pick Image from Gallery and Capture from Camera in Android Studio using Kotlin.
Last updated Dec 18, 2021In this article, we will see how to pick image from gallery and capture image from camera and set to imageview in Android Studio by using Kotlin Language.
Implementation:
Step 1: Create a new Project in android studio.
Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish |
Step 2: Go to activity_picker.xml file and add the following code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <ImageView <Button <Button </LinearLayout> |
Step 3: Add following permissions in AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
Step 4: Add provider inside application tag in AndroidManifest.xml file
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider> |
Step 5: Create a new Directory in res folder (name: "xml") and inside xml create a new xml resource file (name: "provider_paths.xml") and add the following code inside provider_paths.xml file
<?xml version="1.0" encoding="utf-8"?> |
Step 6: Open PickerActivity.kt file and add the following code.
private var imageView: ImageView? = null override fun onCreate(savedInstanceState: Bundle?) { initializeWidgets() btnCapture.setOnClickListener{capturePhoto()} private fun initializeWidgets() { private fun show(message: String) { val intent = Intent("android.media.action.IMAGE_CAPTURE") override fun onRequestPermissionsResult(requestCode: Int, permissions: Array override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
Step 7: Run the app on emulator or real device, you will get the output as following in video
Complete Source Code of Image Picker and Capture from Camera Example
activity_picker.xml file
<?xml version="1.0" encoding="utf-8"?> <ImageView <Button <Button </LinearLayout>
|
PickerActivity.kt file
import android.annotation.SuppressLint class PickerActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { initializeWidgets() btnCapture.setOnClickListener{capturePhoto()} private fun initializeWidgets() { private fun show(message: String) { val intent = Intent("android.media.action.IMAGE_CAPTURE") override fun onRequestPermissionsResult(requestCode: Int, permissions: Array override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
provider_paths.xml file
<?xml version="1.0" encoding="utf-8"?> |
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?> <application <category android:name="android.intent.category.LAUNCHER" /> <provider </manifest> |
Conclusion: In this article we have covered how to Pick Images from Gallery and Capture Image from Camera in Android Studio by using Kotlin Language.
Article Contributed By :
|
|
|
|
4258 Views |