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

Ali Kamalizade
2 min readSep 2, 2018

Android 7 Nougat and higher have new security restrictions which makes Uri.parse() unusable to get access to files in internal or external storage. Instead, we need to use the FileProvider class which is available in the Android Support Library. In this tutorial, I show you how to use FileProvider to access files in internal and external storage.

How to open a file in another app with FileProvider

  1. If you do not include the Android Support Library in your build.gradle yet, then you need to so so we can use FileProvider.
  2. Create a paths.xml in your xml folder. If you do not have this folder, you can create it with Android Studio. Here, you specify to which file paths your FileProvider should grant you access.
  3. Specify the default FileProvider or create your own CustomFileProvider which inherits from FileProvider in AndroidManifest.xml.
  4. Do not forget to declare your needed permission in AndroidManifest.xml! If you want to open a file (e.g. a video), you need the permission to access the external storage. Otherwise, the file access may fail silently.
  5. Use the default FileProvider or your CustomFileProvider to get your files.

The code

AndroidManifest.xml: we specify our provider. Make sure to set exported to false and grantUriPermissions to true! If you want to create a custom FileProvider (a class which extends FileProvider), then you need to change the name of the provider.

<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>

provider_paths.xml: depending on which storage you need to access, you need to use something else than external-path. See the documentation for other options.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

YourActivitity.java: in this example, I want to open a file in a video player app. We either use the default FileProvider or our custom FileProvider we created earlier. For context, you can use this if you are inside an activity. Make sure to call grantUriPermission and add the appropriate flag to your Intent.

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

Thank you for reading this tutorial about opening a file in another app using FileProvider. As you can see, the process is fairly easy once you have completed the FileProvider setup. Let me know in the comments if this tutorial helped you.

--

--

Ali Kamalizade
Ali Kamalizade

Written by Ali Kamalizade

Co-founder of Sunhat. Posts about software engineering, startups and anything else. 有難うございます。🚀

Responses (6)