Muhammad Faisal Amin avatar

Profile Image | Android App Development | Lecture#58 | Hive Learners

faisalamin

Published: 28 Aug 2022 › Updated: 28 Aug 2022Profile Image | Android App Development | Lecture#58 | Hive Learners

Profile Image | Android App Development | Lecture#58 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

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.

alt

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"/>

alt

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" />

alt

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.

alt

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);
                        }
                    }
                });

alt

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);

        });

alt

Let's run the app and allow Permission manually. Open app info and open the permissions.

alt

Click on the camera and select Allow.

alt

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.

alt

hl_divider.png

Thank You

hl_footer_banner.png

Leave Profile Image | Android App Development | Lecture#58 | Hive Learners to:

Written by

Android | Python | Software Engineer

Read more #hive-153850 posts


Best Posts From Muhammad Faisal Amin

We have not curated any of faisalamin's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Muhammad Faisal Amin