![]() |
VOOZH | about |
The purpose of this article is to show how to open a Camera from inside an App and click the image and then display this image inside the same app. An android application has been developed in this article to achieve this. The opening of the Camera from inside our app is achieved with the help of the ACTION_IMAGE_CAPTURE Intent of MediaStore class.
This image shows the Image clicked by the camera and set in Imageview. When the app is opened, it displays the "Camera" Button to open the camera. When pressed, ACTION_IMAGE_CAPTURE Intent gets started by the MediaStore class. When the image is captured, it is displayed in the image view.
To create a new project in the Android Studio, please refer to How to Create/Start a New Project in Android Studio?
Navigate to app > manifests > AndroidManifest.xml and add the following code under the <application> tag.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Navigate to app > res > xml, right click on the folder and select, New > XML Resource File and set the name as file_paths.xml. Now add the following code to the file
file_paths.xml:
Navigate to Gradle Scripts > build.gradle.kts (Module :app) and the following dependency under the dependencies{} scope.
dependencies {
...
implementation("com.github.bumptech.glide:glide:4.16.0")
}
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
Also, Assign the ID to each component along with other attributes as shown in the image and the code below.
android:id="@+id/id_name"Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. We will instantiate the components made in the XML file (Camera Button, ImageView) using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
val object: ComponentType = findViewById(R.id.IdOfTheComponent)val openCamera: Button = findViewById(R.id.camera_open)
val clickedImage: ImageView = findViewById(R.id.click_image)
Add the listener to the Camera button. This will be used to open the camera when the user clicks on the button.
openCamera.setOnClickListener { }Now, create an activity result launcher to open camera.
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
// load image
}
Now use the activity result launcher to get the result, here is the captured image.
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
Then set the image received as a result of Camera intent in the ImageView for display.
Glide.with(this).load(photoUri).into(clickedImage)