Skip to content Skip to sidebar Skip to footer

Capturing And Saving Videos In Android (api > 24) Using File Object?

I want to record a video and then save it once it is done recording for 5 seconds. I have the following code which works fine on API < 24, however for API > 24 I get the erro

Solution 1:

You can use FileProvider class to give access to the particular file or folder to make them accessible for other apps. Create your own class inheriting FileProvider in order to make sure your FileProvider doesn't conflict with FileProviders declared in imported dependencies as described here.

Steps to replace file:// URI with content:// URI:

Add a class extending FileProvider

publicclassGenericFileProviderextendsFileProvider {}

Add a FileProvider <provider> tag in AndroidManifest.xml under <application> tag. Specify a unique authority for the android:authorities attribute to avoid conflicts, imported dependencies might specify ${applicationId}.provider and other commonly used authorities.

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"...
    <application...
        <providerandroid:name=".GenericFileProvider"android:authorities="${applicationId}.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider></application></manifest>

Then create a provider_paths.xml file in res/xml folder. Folder may be needed to created if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.

<?xml version="1.0" encoding="utf-8"?><pathsxmlns:android="http://schemas.android.com/apk/res/android"><external-pathname="external_files"path="."/></paths>

The final step is to change the line of code below in

fileUri = Uri.fromFile(mediaFile);

to

fileUri= FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", mediaFile);

Post a Comment for "Capturing And Saving Videos In Android (api > 24) Using File Object?"