Capture Image From Your Camera Using File Provider In Android

Posted By : Akash Tomer | 30-Apr-2018

Almost all the android application use camera to capture the image either to upload the profile picture or to save some documents of the user. While capturing the image from the camera the file URI is exposed which breaches the android security. So from Nougat, Google has introduced the file provider to capture image from the camera which hides the file URI of the application.Below, I am discussing how we can use file provider in android.

First, You have to add permission in the manifest file 
...
      
"android.hardware.camera"
                  android:required="true" />
...

In this , I use requried tag which is true to tell Google play to load all camera application. If it is false then we have to add the camera permission.We also have to add the write external storage permission to save image.

<manifest>

<uses-permission
       android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Now to save the image and send it to the server we have to create a file. Just call this method which retun the file object which is send it to the server. Below is the code which create a image file .

String imagePath;
private File createImageFile() throws IOException {
    String timeStamp = 
         new SimpleDateFormat("yyyyMMdd_HHmmss", 
                      Locale.getDefault()).format(new Date());
    String imageFileName = "IMG_" + timeStamp + "_";
    File storageDir =  
                getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
                    imagename, 
                    ".jpg",       
                    imageDir     
    );

    imagePath = image.getAbsolutePath();
    return image;
}

Now capture the image using camera Intent.To access the file, we call the file provider which provide the URI to access the file.

private static final int REQUEST_CAPTURE_IMAGE = 200;

private void openCameraIntent() {
   Intent pictureIntent = new Intent(
                              MediaStore.ACTION_IMAGE_CAPTURE);
   if(pictureIntent.resolveActivity(getPackageManager()) != null){ 
        //Create a file to store the image
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
        }      
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,                                                                                                    "com.example.android.provider", photoFile);
            pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                         photoURI);
            startActivityForResult(pictureIntent,
                                     REQUEST_CAPTURE_IMAGE);
        }
   }
}

Add the following line of code in the Manifest file.There is a tag provider which help to restrict the application to access files of specified path mentioned in the resorce file i.e xml file privider. 

application>
   ...
   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths">
        </meta-data>
    </provider>
    ...
</application>

In this you have to create the xml provide file which tell the path of image URI in android. Create a xml file and following lines of code.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images"  
      path="Android/data/com.example.package.name/files/Pictures" />
</paths>

 

 

Hope this will help you.

About Author

Author Image
Akash Tomer

Akash is an Android Developer at Oodles Technology.

Request for Proposal

Name is required

Comment is required

Sending message..