1.2 Project Setup
In this lesson, you’ll create a new Android Studio project and configure it so that it has all the required assets, dependencies and permissions for the course project. You’ll also learn how to support devices running Android 6.0 or higher by explicitly requesting users to grant the required permissions during run-time.
1.Introduction2 lessons, 06:13
2.Using the Camera2 API2 lessons, 26:08
3.Using Android GPUImage2 lessons, 10:44
4.Conclusion1 lesson, 00:30
1.2 Project Setup
Hello and welcome back. In this lesson we create a new Android Studio project and configure it, so that it has all the required permissions, dependencies, and assets. I'll be using Android Studio2.1.1 throughout this course. All right, let's start by selecting the Start a new Android Studio project option in the welcome menu. I'm going to name this application My Camera. The camera to API was introduced in Android 5.0. Therefore, the Minimum SDK we can support here is API level 21. Choose the empty activity in the screen and press Next and then press Finish. Once the project has been generated, open its manifest file. Here, we must add a uses-permission tag in order to get the permission to use the camera. Set the name attribute of the tag to android.permission.CAMERA. Similarly add another uses-permission tag in order to get the permission to write to the external storage. This is required because we'll be storing the photos we capture on external storage media such as SD cards. To keep this app simple, we won't be handling any device orientation changes. So set the screen orientation attribute of main activity to portrait. This way, the activity will always be in the portrait mode. Next, open build.gradle so that we can specify the libraries we'll be using in this project. The first library we are gonna need is the design support library. So copy this line, paste it and just change appcompat-v7 to design. Next, we'll be using a popular library called Android GPUImage. This library allows us to apply various image filters to the photos. To add it, just copy this line here and paste it in your build.gridle file, press Sync Now to sync the dependencies we added. Let's add a vector asset to our project now. Open Vector Asset Studio and click on the choose button. Open the image category here and select the camera icon, press OK > Next and Finish. At this point, our project is fully configured for devices running API level 22 or lower. Now in order to support devices running API level 23 or higher, we must request the permissions be mentioned in a manifest file again during runtime. So open main-activity.java. Here create a new method called requestPermissions. Inside the method, check if the user has granted the required permissions using the context compact.checkselfpermission method. So first check if the result for the camera permission is equal to PackageManager.PERMISSION_GRANTED. And then check if the result for the right external storage permission is equal to PackageManager.PERMISSION_GRANTED. If you have both permissions, we don't have to do anything for now, but if you don't have those permissions, we must ask for them using the ActivityCompat.requestPermissions method. Pass both the permissions to the method in the form of a string array. We must also pass a request code to it. I'm gonna call it MY_REQUEST_CODE and make it a constant field of the activity. Its value can be any integer, I'll say 1. To handle the result of our request, we must override a method called onRequestPermissionsResult. Here, check if the request code is equal to MY_REQUEST_CODE. And then check if the grantResults array has two results. Next, check if both the results are equal to PackageManager.PEMISSION_GRANTED. If that is true, we don't have to do anything for now. But if it is not true, it means that the user has not granted us the permissions we need. So let us create a toast message using the Toast.makeText method saying Permissions not granted. And then call it show method to display the message. Our app cannot run without the permission. So at this point, call finish to close the app. If you press the run button now and choose a device running API level 23 or higher, You're going to see something like this as soon as the app starts. This brand is of course for the camera permission. Press allow and this prompt is for the right external storage permission. Press Allow again. Our project setup is now complete. In the next lesson, we'll start using the camera to API to create a live preview interface. I'll be waiting for you there. Thanks for watching.