I am using the file SaveAndroid.cs as is but when I step through the code on my Android emulator, I get the following error:
file:///storage/emulated/0/***/****.docx exposed beyond app through Intent.getData()
Android.Net.Uri path = Android.Net.Uri.FromFile(file);
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(path, mimeType);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
The exception is thrown on the last line above. In the above code, the mimeType =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Hi Sree,
From the given details, we suspect that the reported problem might be due to file://
URI path used for view the generated document.
If your targetSdkVersion >= 24, then we have to use FileProvider class to
give access to the particular file or folder to make them accessible for other
apps.
To resolve the reported problem, we suggested you to replace file:// URI with
content:// URI path in SaveAdnroid.cs
Steps to replace file:// URI with content:// URI:
1. Add a FileProvider <provider> tag in AndroidManifest.xml
under <application> tag.
|
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.create_word_document"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" /> <application android:label="Create_Word_document.Android" android:theme="@style/MainTheme"> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider> </application> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> |
2. Then create a provider_paths.xml file in Resources/xml folder.
|
<?xml version="1.0" encoding="UTF-8" ?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths> |
3. The final step is to change the line of code below in SaveAndroid.cs
|
//Invoke the created file for viewing if (file.Exists()) { Android.Net.Uri path = Android.Net.Uri.FromFile(file); string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString()); string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); Intent intent = new Intent(Intent.ActionView); intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask); path = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file); intent.SetDataAndType(path, mimeType); intent.AddFlags(ActivityFlags.GrantReadUriPermission); Android.App.Application.Context.StartActivity(intent); } |
Please refer the attached complete sample
application.
Could you please try the above solution and let
us know whether the reported issue gets resolved?
We will update this code in our documentations and
examples also.
Regards,
Suriya Balamurugan.
Thank you Suriya. The solution worked for both Word and PowerPoint files.