video app to android marshmallow

For this tutorial we describe how to upgrade the android video app to android marshmallow. For this android tutorial we will be supporting read external storage.

The android video application requires two permissions to function, read external storage access to load the video and the access to an external camera application for the video capture.

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

This tutorial will describe the steps required to provide the read external storage using the new android marshmallow runtime permissions.

Get Code

The code is now on github you can get it from here Tag videoapp-marshmallow

or else run this command

git clone –branch videoapp-marshmallow

Code Samples

Change SDK version & add appcompat v7 library support


apply plugin: ''

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.nigelhenshaw.videoapp"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ""

Add the read external storage permission to the android manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >

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

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



Add an activity member for the read external storage request

private static final int REQUEST_READ_STORAGE_RESULT = 1;

Add an activity member to the Uri which will be used to load the video

private Uri mVideoUri;

Add the runtime permission check for read external storage in the activity callback

Before we can read the Uri representing the video we need to check to see if we have permission to read the file

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
           mVideoUri = data.getData();

           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
               if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
                       PackageManager.PERMISSION_GRANTED) {
               } else {
                   if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                               "App needs permissions to play the video", Toast.LENGTH_SHORT).show();
                   requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
           } else {


In the request permission callback check to see if permission was granted and then set the video url for the video view

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(requestCode == REQUEST_READ_STORAGE_RESULT) {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            } else {
                        "Unable to play video because don't have permission to access file", Toast.LENGTH_SHORT).show();
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
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>