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

https://github.com/mobapptuts/video_app.git Tag videoapp-marshmallow

or else run this command

git clone –branch videoapp-marshmallow https://github.com/mobapptuts/video_app.git

Code Samples

Change SDK version & add appcompat v7 library support

build.gradle

apply plugin: 'com.android.application'

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'), 'proguard-rules.pro'
        }
    }
}

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

Add the read external storage permission to the android manifest file

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nigelhenshaw.com.videoapp" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".VideoAppMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

VideoAppMainActivity

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) {
                   mVideoView.setVideoURI(mVideoUri);
               } else {
                   if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                       Toast.makeText(this,
                               "App needs permissions to play the video", Toast.LENGTH_SHORT).show();
                   }
                   requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
                           REQUEST_READ_STORAGE_RESULT);
               }
           } else {
               mVideoView.setVideoURI(mVideoUri);
           }


       }
   }

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

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(requestCode == REQUEST_READ_STORAGE_RESULT) {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mVideoView.setVideoURI(mVideoUri);
            } else {
                Toast.makeText(this,
                        "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>