Introduction
Most mobile applications have the feature of accessing the camera to capture photos and videos. The integration of camera and accessing permissions for the camera is discussed in this article.
Permissions
Permission is asked at the time of accessing the camera to store the images or videos. For this, external storage permission is required. To record audio, audio permission is required. The android. hardware. camera feature is used to access the device camera and flash package is used to access flash while taking pictures.
Configuring the Emulator
The development of the application is performed in an emulator and the configuration is set to access the camera.
Select the main menu and open the android emulator manager.
Pick the emulator used for development and select edit.
Select the type of camera as front or back camera in the edit AVD dialog.
Data Service
The camera is used to capture images and the captured picture need to be stored in a location if the device and data service is not required to store the image in a certain location. Data service deletes the POI and also deletes the corresponding POI image. The below code is used to add data services:
- public interface IPOIDataService
- {
- string GetImageFilename ( int id );
- }
Implementing GetImageFilename()
After the image is captured it is stored in a particular location with a unique file name. The below function is used to store the image in a location with a unique file name.
- public string GetImageFilename ( int id )
- {
- return Path.Combine (_storagePath, "poiimage" + id.ToString () + ".jpg" );
- }
To delete an image that corresponds to POI, the DeletePOI method is used and the below code is used to delete the image that corresponds to the POI if it exists.
- pubic void DeletePOI ( PointOfInterest poi )
- {
- if ( File.Exists (GetFilename (poi.Id)) )
- File.Delete (GetFilename (poi.Id) );
-
- if ( File.Exists (GetImageFilename (poi.Id )))
- File.Delete (GetImageFilename (poi.Id ));
-
- _pois.Remove (poi);
- }
POIDetailActivity
Capturing the image from POIDetailActivity has some tasks shown below, step by step.
Add a new user interface widgets to initiate capturing and displaying the photo.
Build a photo intent to navigate to the external camera app to capture the photo.
Processing the result of photo intent and displaying the photo if the picture is captured successfully.
Adding User Interface Elements
There are many user interface elements to capture the picture and displaying the component to display the captured image. Add the new imageButton element and add the Image view element to the screen. The sample code is shown below to display the image view on the screen.
- < /TableRow>
- < /TableLayout>
- < ImageView
- p1 : src = "@android : drawable/ic_menu_gallery"
- p1 : layout_width = "wrap_content"
- p1 : layout_height = "wrap_content"
- p1 : padding = "10dp"
- p1 : id = "@+id/poiImageView"
- p1 : layout_gravity = "center _ horizontal "
- p1 : scaleType = "fitCenter" />
- < LinearLayout>
Intent
The intent class is used to start the external camera app to capture the images and the Mediastore.ActionImageCapture action shows the android platform to capture a photo and to use the existing app. The below line is used to create an intent with image capture action.
- Intent cameraIntent = new intent ( MediaStore.ActionImageCapture );
The startActivityForResult is a method work in conjunction with a callback to OnActivityResult to communicate the result of the intent. It is an int value named request code that returns a parameter in the callback to OnActivityResult and helps identify for launching the intent. The below line is used to start the intent.
- startActivityForResult ( cameraIntent , CAPTURE_PHOTO ) ;
Displaying Image in POIDetailActivity
Select the same POI, the previously captured image is not displayed and the onCreate method is used to load the image if an ID for POI was passed in the intent.
- if (Intent.HasExtra ("poiId"))
- {
- int poiId = Intent.GetIntExtra ("poiId" , -1);
- _poi = POIData.Service.GetPOI (poiId);
- Bitmap PoiImage = POIDatta.GetImageFile (_poi.Id.Value);
- _poiImageView.SetImageBitmap (poi Image);
- }
- else
- _poi = new PointOfInterest ();