Image for post
Image for post

How to open a file in another app with FileProvider for Android 7+

How to open a file in another app with FileProvider

The code

<application>
...
<!-- Needed for Android >= Nougat for file access -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.myprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
...
</application>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
final Uri data = FileProvider.getUriForFile(context, "myprovider", new File(file_path));
context.grantUriPermission(context.getPackageName(), data, Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(data, "video/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);

Conclusion

Written by

Senior Software Engineer @LeanIX. Co-founder of Sedeo. Passion for software engineering and startups. Looking forward to build great things. 有難うございます。🚀

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store