Camera-intent to android marshmallow

For this tutorial we describe how to upgrade the camera-intent to android marshmallow. Which will adding runtime permissions for writing to external storage.

The security permissions in android marshmallow have gone through significant changes where the user now grants permissions while using the application instead of the old way which was to accept a list  of permissions prior to installing the application.

The new permission model does require more work and thought from the developer in how to present  the required permissions to the user but does provide the user more choice with what data and personal information they want to share with the application.

The camera intent application requires two permissions to function, the write access to external storage and the access to an external camera application.

Because the application will be requesting access to the android camera application via an android intent no runtime permissions will be required for the camera, just write access to external storage.

This tutorial will describe the steps required to provide the write access to external storage runtime permission.

Get Code

The code is now on github you can get it from here

https://github.com/mobapptuts/camera_intent.git Tag camera-intent-marshmallow

or else run this command

git clone –branch camera-intent-marshmallow https://github.com/mobapptuts/camera_intent.git

Code Samples

Change SDK version & add appcompat v7 library support

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.mobapptuts.cameraintent"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.android.support:appcompat-v7:23.1.1"
}

Add runtime permissions for write external storage

CamaraIntentActivity.java

Add request code

private static final int REQUEST_EXTERNAL_STORAGE_RESULT = 1;

Create method for calling camera app via intent

private void callCameraApp() {
    Intent callCameraApplicationIntent = new Intent();
    callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

    File photoFile = null;
    try {
        photoFile = createImageFile();

    } catch (IOException e) {
        e.printStackTrace();
    }
    callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

    startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);

}

Implement the runtime permission request

public void takePhoto(View view) {
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
           if (ContextCompat.checkSelfPermission(this,
                   Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
               callCameraApp();
           } else {
               if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                   Toast.makeText(this,
                           "External storage permission required to save images",
                           Toast.LENGTH_SHORT).show();
               }
               requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                       REQUEST_EXTERNAL_STORAGE_RESULT);
           }
       } else {
           callCameraApp();
       }
   }

Setup the callback for the result of the permission request

@Override
   public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
       if(requestCode == REQUEST_EXTERNAL_STORAGE_RESULT) {
           if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               callCameraApp();
           } else {
               Toast.makeText(this,
                       "External write permission has not been granted, cannot saved images",
                       Toast.LENGTH_SHORT).show();
           }
       } else {
           super.onRequestPermissionsResult(requestCode, permissions, grantResults);
       }
   }

How to revoke the android marshmallow runtime permissions

The command to revoke the android marshmallow runtime permissions is in this format

adb shell pm revoke com.mobapptuts.cameraintnet android.permission.WRITE_EXTERNAL_STORAGE
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>