Android.OS.FileUriExposedException since updating android to API 28

TobyKTobyK GBMember ✭✭✭

Our app offers the users to 'Download Latest Version' whenever we roll out an updated version of the app on our own servers. We've recently updated the Target to 28 (Android 9.0) and the following code now causes the Android.OS.FileUriExposedException as follows:

            Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/download/" + filename)), "application/vnd.android.package-archive");
            promptInstall.AddFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(promptInstall);

The result:

[0:] Android.OS.FileUriExposedException: file:///storage/emulated/0/download/app.apk exposed beyond app through Intent.getData()
  at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <034d4a9852dd45bea9353cc7776c99f0>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <034d4a9852dd45bea9353cc7776c99f0>:0 
  at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent intent) [0x00031] in <a10f61e70eeb434e952fef884856c199>:0 
  at App.Droid.FileInteractions.SaveBinary (System.String filename, System.Byte[] bytes) [0x0004a] in C:\tr\App\App\App.Droid\FileInteractions.cs:20 
  at App.ViewModels.SettingsViewModel.client_DownloadUpdateCompleted (System.Object sender, App.TmService.DownloadUpdateCompletedEventArgs e) [0x000b6] in C:\tr\App\App\App\ViewModels\SettingsViewModel.cs:475 
  --- End of managed Android.OS.FileUriExposedException stack trace ---
android.os.FileUriExposedException: file:///storage/emulated/0/download/app.apk exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
    at android.net.Uri.checkFileUriExposed(Uri.java:2351)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:9043)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:9004)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
    at android.app.ContextImpl.startActivity(ContextImpl.java:791)
    at android.app.ContextImpl.startActivity(ContextImpl.java:768)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)

Could some one help me with this please? My android knowledge is limited and I can't seem to find a 'workable' solution to this problem that doesn't involve half a day to implement...

Answers

Sign In or Register to comment.