How To Use Camera API For Android N And Above Devices In Android

How To Use Camera API For Android N And Above Devices In Android
 

Introduction

 
In my previous article, we learned how to handle “android.os.FileUriExposedException” if we have an app that shares files with other apps using a URI on API 24+. In this article, we will learn how to use Camera Intent in Android N and above devices.
 
Creating a New Project with Android Studio
  1. Open Android Studio and select "Create new project".
  2. Name the project as per your wish and select your activity template.
     
    How To Use Camera API For Android N And Above Devices In Android
     
  3. Click the finish button to create a new project in Android Studio.

Steps to use Camera API

  • We already know how to open the camera from our app using intents. But for Android N & the above devices, Google has changed the approach for accessing the Camera API. In this step, we will see how to use Camera Intent for Pre Android N and Android N above devices. 
     
  • Open your MainActivity.java file and add a button with a click event to open the Camera. The following code snippet shows the camera intent. 
    1. btnPickCamera.setOnClickListener(new View.OnClickListener() {    
    2.     @Override    
    3.     public void onClick(View view) {    
    4.         // Checking Permission for Android M and above    
    5.         if (ActivityCompat.checkSelfPermission(MainActivity.this,    
    6.                 Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED    
    7.                 || ActivityCompat.checkSelfPermission(MainActivity.this,    
    8.                 Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {    
    9.             ActivityCompat.requestPermissions(MainActivity.this,    
    10.                     new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_CAMERA);    
    11.             return;    
    12.         }    
    13.     
    14.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {    
    15.             ContentValues values = new ContentValues(1);    
    16.             values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");    
    17.             outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);    
    18.             Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    19.             captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);    
    20.             captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);    
    21.             startActivityForResult(captureIntent, PICK_FROM_CAMERA);    
    22.         } else {    
    23.             Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    24.             File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");    
    25.             outputFileUri = Uri.fromFile(file);    
    26.             captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);    
    27.             startActivityForResult(captureIntent, PICK_FROM_CAMERA);    
    28.         }    
    29.     }    
    30. });    
  • Here, I have added runtime permission checking for Android N and the above devices.
    1. ContentValues values = new ContentValues(1);    
    2. values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");    
    3. outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);    
    4. Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    5. captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |       Intent.FLAG_GRANT_WRITE_URI_PERMISSION);    
    6. captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);    
    7. startActivityForResult(captureIntent, PICK_FROM_CAMERA);   
  • Here, we have created ContentValues and added MIME type as image. Then, created an output URI for the Camera API output image.
     
  • Then, we have added flags “FLAG_GRANT_READ_URI_PERMISSION” and “FLAG_GRANT_WRITE_URI_PERMISSION” to allow the Camera API to take an image in Android N & above devices.
     
  • We can retrieve the picked camera image by “onActivityResult” and the same as the old approach.
Full Code
 
The following code shows how to access an image from Gallery, how to take pictures from the camera, and how to open a file using intent in Android N & above devices.
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     ImageView imgPreview;  
  4.     TextView imgPath;  
  5.     Button btnPickCamera;  
  6.     Button btnPickGallery;  
  7.     Button btnOpenFile;  
  8.   
  9.     Uri outputFileUri;  
  10.     private static final int PICK_FROM_CAMERA = 1;  
  11.     private static final int PICK_FROM_GALLERY = 2;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         initViews();  
  18.         initOperations();  
  19.     }  
  20.   
  21.     private void initViews() {  
  22.         imgPreview = findViewById(R.id.imgPreview);  
  23.         imgPath = findViewById(R.id.imgPath);  
  24.         btnPickCamera = findViewById(R.id.btnCapture);  
  25.         btnPickGallery = findViewById(R.id.btnGallery);  
  26.         btnOpenFile = findViewById(R.id.btnOpenImg);  
  27.     }  
  28.   
  29.     private void initOperations() {  
  30.         btnPickGallery.setOnClickListener(new View.OnClickListener() {  
  31.             @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
  32.             @Override  
  33.             public void onClick(View view) {  
  34.                 // Checking Permission for Android M and above  
  35.                 if (ActivityCompat.checkSelfPermission(MainActivity.this,  
  36.                         Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {  
  37.                     ActivityCompat.requestPermissions(MainActivity.this,  
  38.                             new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);  
  39.                     return;  
  40.                 }  
  41.                 Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  42.                 // Start the Intent  
  43.                 startActivityForResult(galleryIntent, PICK_FROM_GALLERY);  
  44.             }  
  45.         });  
  46.   
  47.         btnPickCamera.setOnClickListener(new View.OnClickListener() {  
  48.             @Override  
  49.             public void onClick(View view) {  
  50.                 // Checking Permission for Android M and above  
  51.                 if (ActivityCompat.checkSelfPermission(MainActivity.this,  
  52.                         Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED  
  53.                         || ActivityCompat.checkSelfPermission(MainActivity.this,  
  54.                         Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {  
  55.                     ActivityCompat.requestPermissions(MainActivity.this,  
  56.                             new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_CAMERA);  
  57.                     return;  
  58.                 }  
  59.   
  60.                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  61.                     ContentValues values = new ContentValues(1);  
  62.                     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");  
  63.                     outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
  64.                     Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  65.                     captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);  
  66.                     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);  
  67.                     startActivityForResult(captureIntent, PICK_FROM_CAMERA);  
  68.                 } else {  
  69.                     Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  70.                     File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");  
  71.                     outputFileUri = Uri.fromFile(file);  
  72.                     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);  
  73.                     startActivityForResult(captureIntent, PICK_FROM_CAMERA);  
  74.                 }  
  75.             }  
  76.         });  
  77.   
  78.         btnOpenFile.setOnClickListener(new View.OnClickListener() {  
  79.             @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)  
  80.             @Override  
  81.             public void onClick(View v) {  
  82.                 // Checking Permission for Android M and above  
  83.                 if (ActivityCompat.checkSelfPermission(MainActivity.this,  
  84.                         Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {  
  85.                     ActivityCompat.requestPermissions(MainActivity.this,  
  86.                             new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);  
  87.                     return;  
  88.                 }  
  89.                 File file = new File(imgPath.getText().toString());  
  90.                 Intent intent = new Intent(Intent.ACTION_VIEW);  
  91.                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);  
  92.                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  93.                     Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);  
  94.                     intent.setDataAndType(apkURI, "image/jpg");  
  95.                     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
  96.                 } else {  
  97.                     intent.setDataAndType(Uri.fromFile(file), "image/jpg");  
  98.                 }  
  99.                 startActivity(intent);  
  100.             }  
  101.         });  
  102.     }  
  103.   
  104.     @Override  
  105.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  106.         super.onActivityResult(requestCode, resultCode, data);  
  107.         Bitmap bitmap;  
  108.         switch (requestCode) {  
  109.             case PICK_FROM_CAMERA:  
  110.                 if (resultCode == Activity.RESULT_OK) {  
  111.   
  112.                     Uri selectedImage = outputFileUri;  
  113.                     ContentResolver cr = getContentResolver();  
  114.                     getContentResolver().notifyChange(selectedImage, null);  
  115.                     try {  
  116.                         bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);  
  117.                         int nh = (int) ( bitmap.getHeight() * (512.0 / bitmap.getWidth()) );  
  118.                         bitmap = Bitmap.createScaledBitmap(bitmap, 512, nh, true);  
  119.                         imgPreview.setImageBitmap(bitmap);  
  120.                         imgPath.setText(outputFileUri.getPath());  
  121.                     } catch (Exception e) {  
  122.                         Toast.makeText(this"Failed to load", Toast.LENGTH_SHORT)  
  123.                                 .show();  
  124.                     }  
  125.                 }  
  126.                 break;  
  127.             case PICK_FROM_GALLERY:  
  128.                 if (resultCode == Activity.RESULT_OK) {  
  129.                     //pick image from gallery  
  130.                     Uri selectedImage = data.getData();  
  131.                     String[] filePathColumn = {MediaStore.Images.Media.DATA};  
  132.                     // Get the cursor  
  133.                     assert selectedImage != null;  
  134.                     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn,  
  135.                             nullnullnull);  
  136.                     // Move to first row  
  137.                     assert cursor != null;  
  138.                     cursor.moveToFirst();  
  139.   
  140.                     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
  141.                     String imgDecodableString = cursor.getString(columnIndex);  
  142.                     cursor.close();  
  143.                     bitmap = BitmapFactory.decodeFile(imgDecodableString);  
  144.                     imgPreview.setImageBitmap(bitmap);  
  145.                     imgPath.setText(imgDecodableString);  
  146.                 }  
  147.                 break;  
  148.         }  
  149.     }  
  150. }  
Reference
 
How to handle “android.os.FileUriExposedException”.
Download
 
You can download the code from GitHub. If you like this article do like & share the article and star the repo in GitHub.


Similar Articles