June 20, 2018

Capture Image Using Camera

       On this blog I am going to Explain How to capture Image. Here I am using FileProvider for Capture Image. Here is the steps to integrate Image Capture using Camera on your application.

Add Camera Permission

         We need to Add the Camera permission on AndroidManifest.xml file like,


<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


Add File Provider

         We need to Add provider inside the application tag on AndroidManifest.xml file like,

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




Add file path

          Inside the  provider tag we added android:resource="@xml/file_paths", we need to add new folder xml(If the folder not exist inside the res folder) and inside the xml folder we need to add new resource file file_paths with the below content,


<?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.android.myapplication/files/Pictures" />
</paths>


Java Code

          Here I am capturing the image using my function takePicture(), Before calling this function I am checking the camera permission using my function checkCameraPermissionGranded(), On the Camera Button Click I am using,


if (!checkCameraPermissionGranded())
    requestCameraPermission();
else                    
    takePicture();


Check Camera Permission
private boolean checkCameraPermissionGranded() {
        if (ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(HomeActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else
            return false;
}


Request Camera Permission


      If the permission is not given we will request permission for access camera for our application using requestCameraPermission() methid. Once user grand permission it will automatically execute override method onRequestPermissionsResult. 


    private void requestCameraPermission() {
        ActivityCompat.requestPermissions(HomeActivity.this, new String[]{android.Manifest.permission.CAMERA,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}, Constants.CAPTURE_REQUEST_CODE);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == Constants.CAPTURE_REQUEST_CODE) {
            if (checkCameraPermissionGranded())
                takePicture();
            else {
                requestCameraPermission();
                Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();
            }
        } else if (requestCode == Constants.LOCATION_REQUEST_CODE) {
            if (!checkLocationPermissionGranded()) {
                requestCameraPermission();
                Toast.makeText(HomeActivity.this, "Permission Not Granded", Toast.LENGTH_LONG).show();
            } else
                location = getUserLocation();
        }

    }


Capture Image

  Using takePicture() method we will capture Image , Once capture the image it will execute the override method  onActivityResult




private void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
        }
        if (photoFile != null) {
            ProfilePicUri = FileProvider.getUriForFile(HomeActivity.this, "com.android.myapplication.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, ProfilePicUri);
            startActivityForResult(takePictureIntent, Constants.IMAGE_CAPTURE_CAMERA);
        }
    }
}




@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.IMAGE_CAPTURE_CAMERA && resultCode == RESULT_OK) {
        if (ProfilePicPath != null && ProfilePicPath.trim().length() > 0 && !ProfilePicPath.isEmpty()) {
            File image_file = new File(ProfilePicPath);
            if (image_file.exists()) {
                Bitmap newProfilePic = BitmapFactory.decodeFile(ProfilePicPath);
                // Code to manage the bitmap
            }
        }
    }
}






---------------

Thanks For Reading, Wish you a Happy Coding....