The error message you're seeing (FileUriExposedException) occurs because starting from Android 7.0 (API level 24), file:// URIs are no longer allowed to be passed to other applications via Intent. This is a security measure to prevent exposing sensitive files to other apps.
Step 1 :
Go to app -> src -> main -> AndroidManifest.xml.
Step 2:
Copy following code:
- In your AndroidManifest.xml file, ensure that you have defined the
FileProvidercorrectly:
- Verify that you have created the
file_paths.xmlfile correctly in theres/xmldirectory with the following content:
<paths>
<external-path name="external_files" path="." />
</paths>
FileProvider to generate a content URI for your PDF file and pass it through the Intent. Here's how you can modify your code to use FileProvider: // Assuming your PDF file path is stored in the variable 'pdfFilePath'File file = new File(String.valueOf(Environment.getExternalStorageDirectory()), "muzamil.pdf");
// Generate a content URI using FileProvider
Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.example.meyepro.fileprovider", file);
Toast.makeText(this, ""+getContext().getPackageName() + ".fileprovider", Toast.LENGTH_SHORT).show();
// Create an 'Intent' to view the PDF
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Grant read permission to the receiving app
// Start the activity with the 'Intent'
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(), "No PDF viewer application found.", Toast.LENGTH_SHORT).show();
}
add this two line in onCreate
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Share method
File dir = new File(Environment.getExternalStorageDirectory(), "ColorStory");
File file = new File(dir, "0.pdf");
Intent openPdfIntent = new Intent(Intent.ACTION_VIEW);
openPdfIntent.setDataAndType(Uri.parse("file://" + file), "application/pdf");
openPdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(openPdfIntent);
In the Java code, we create an Uri object based on the SDK version. If the SDK version is below 24, we use Uri.fromFile(file). Otherwise, we use Uri.parse(file.getPath()) to get the URI of the file.
Then, we create an Intent with the ACTION_SEND action and set the appropriate MIME type for a PDF file. We add the URI as an extra with Intent.EXTRA_STREAM. Additionally, we set the subject and text for the sharing message.
Finally, we use Intent.createChooser to create a chooser dialog and start the activity with the chosen intent.
Please note that you may need to handle runtime permissions and file access based on your specific use case and target SDK version.
File dir = new File(Environment.getExternalStorageDirectory(), "ColorStory");
File file = new File(dir, "0.pdf");
Uri uri;
if (Build.VERSION.SDK_INT < 24) {
uri = Uri.fromFile(file);
} else {
uri = Uri.parse(file.getPath());
}
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Purchase Bill...");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Sharing Bill purchase items...");
startActivity(Intent.createChooser(shareIntent, "Share Via"));