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.

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.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
7 comments
avatar

this is nice brother... you can use Vscode IDE to do this right. I envy you man, this is some stuffs I would love to learn some day.

0
0
0.000
avatar

Yeah we can use VS Code but Android Studio IDE is best for java and kotlin

0
0
0.000
avatar

This is cool, I would have loved to give this a try but am not much of a computer guy

0
0
0.000
avatar

Thanks brother, You can try it is simple,

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar

Congratulations @faisalamin! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Day - September 1st 2022
0
0
0.000