+ // Create a file saved snackbar.
+ Snackbar fileSavedSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.file_saved) + " " + filePathString, Snackbar.LENGTH_LONG);
+
+ // Add an open action if the file is not an APK on API >= 26 (that scenario requires the REQUEST_INSTALL_PACKAGES permission).
+ if (!(Build.VERSION.SDK_INT >= 26 && filePathString.endsWith(".apk"))) {
+ fileSavedSnackbar.setAction(R.string.open, (View view) -> {
+ // Get a file for the file path string.
+ File file = new File(filePathString);
+
+ // Declare a file URI variable.
+ Uri fileUri;
+
+ // Get the URI for the file according to the Android version.
+ if (Build.VERSION.SDK_INT >= 24) { // Use a file provider.
+ fileUri = FileProvider.getUriForFile(context, activity.getString(R.string.file_provider), file);
+ } else { // Get the raw file path URI.
+ fileUri = Uri.fromFile(file);
+ }
+
+ // Get a handle for the content resolver.
+ ContentResolver contentResolver = context.getContentResolver();
+
+ // Create an open intent with `ACTION_VIEW`.
+ Intent openIntent = new Intent(Intent.ACTION_VIEW);
+
+ // Set the URI and the MIME type.
+ if (filePathString.endsWith("apk") || filePathString.endsWith("APK")) { // Force detection of APKs.
+ openIntent.setDataAndType(fileUri, MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk"));
+ } else { // Autodetect the MIME type.
+ openIntent.setDataAndType(fileUri, contentResolver.getType(fileUri));
+ }
+
+ // Allow the app to read the file URI.
+ openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+
+ // Show the chooser.
+ activity.startActivity(Intent.createChooser(openIntent, context.getString(R.string.open)));
+ });
+ }
+
+ // Show the file saved snackbar.
+ fileSavedSnackbar.show();