𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hello Hive Learners, In the previous lecture we learn how to implement the services in our android app. We show a start and stop message. Today we will learn how to open the camera in our app and get the result in an image view. It is easy and this time we will use manual permission allow. We will manually allow the camera permission for our app. We will use this image as Profile Image during the Signup.
GitHub Link
Use this GitHub project to clone into your directory. The following lecture will update it so you will never miss the latest code. Happy Coding!
What Should I Learn
- How to open Camera in App
- How to get the result to an ImageVIew
Assignment
- Get the result to an ImageVIew
Procedure
The old method was very simple we start the intent like startActivityForResult and it overrides a result method and we get the camera data in it. But the new method is different. As we are moving forward in Android Api Levels some methods need to change. First of all, we need to add the Camera Permission to our Android Manifest file.
<uses-permission android:name="android.permission.CAMERA"/>
Now we need to add an ImageView to our SignUp activity XML file. Also, set a unique id for it.
<ImageView
android:id="@+id/profileImg_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="5dp" />
We are ready to implement the code for it. We will use this image on Click listener to open the camera. In the future, we will store it as the User Profile Image in our Firebase Database. Declare and initialize this ImageView in the Signup activity and set the On Click listener.
Now we need to write the code that will get a result. We will add this before the click listener, and get and set the result to our profile_image.
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Bitmap image = (Bitmap) result.getData().getExtras().get("data");
profile_imageA.setImageBitmap(image);
}
}
});
Now on button click, we need to write the intent code to open the Camera.
profile_image.setOnClickListener(view -> {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
someActivityResultLauncher.launch(intent);
});
Let's run the app and allow Permission manually. Open app info and open the permissions.
Click on the camera and select Allow.
Now run the app and click on the imageView. We have not set the placeholder image so we need to click on the empty center space. If the system asks for permission click on Allow this time or always allow. It will open the Camera in Image Capture Mode. Take a picture and select it. It will set that image to our profile_image view.

Thank You
