public void Get_Existing_External_Shareable_Uri(FileProviderLocation location) { // Save an external directory file var root = Platform.AppContext.GetExternalFilesDir(null).AbsolutePath; var file = CreateFile(root); // Make sure it is where we expect it to be Assert.True(FileProvider.IsFileInPublicLocation(file)); // Actually get a safe shareable file uri var shareableUri = GetShareableUri(file, location); // Make sure the uri is what we expected Assert.NotNull(shareableUri); Assert.Equal("content", shareableUri.Scheme); Assert.Equal("com.microsoft.maui.essentials.devicetests.fileProvider", shareableUri.Authority); if (OperatingSystem.IsAndroidVersionAtLeast(29)) { #pragma warning disable CS0618 // Type or member is obsolete var externalRoot = AndroidEnvironment.ExternalStorageDirectory.AbsolutePath; #pragma warning restore CS0618 // Type or member is obsolete // replace the real root with the providers "root" var segements = Path.Combine(root.Replace(externalRoot, "external_files", StringComparison.Ordinal), Path.GetFileName(file)); Assert.Equal(segements.Split(Path.DirectorySeparatorChar), shareableUri.PathSegments); } }
public void Get_Existing_External_Shareable_Uri(FileProviderLocation location) { // Save an external directory file #if !__ANDROID_29__ var externalRoot = AndroidEnvironment.ExternalStorageDirectory.AbsolutePath; #endif var root = Platform.AppContext.GetExternalFilesDir(null).AbsolutePath; var file = CreateFile(root); // Make sure it is where we expect it to be Assert.True(FileProvider.IsFileInPublicLocation(file)); // Actually get a safe shareable file uri var shareableUri = GetShareableUri(file, location); // Make sure the uri is what we expected Assert.NotNull(shareableUri); Assert.Equal("content", shareableUri.Scheme); Assert.Equal("com.xamarin.essentials.devicetests.fileProvider", shareableUri.Authority); #if !__ANDROID_29__ // replace the real root with the providers "root" var segements = Path.Combine(root.Replace(externalRoot, "external_files"), Path.GetFileName(file)); Assert.Equal(segements.Split(Path.DirectorySeparatorChar), shareableUri.PathSegments); #endif }
static Android.Net.Uri GetShareableUri(string file, FileProviderLocation location) { try { // use the specific location FileProvider.TemporaryLocation = location; // get the uri return(FileSystemUtils.GetShareableFileUri(new ReadOnlyFile(file))); } finally { // reset the location FileProvider.TemporaryLocation = FileProviderLocation.PreferExternal; } }
static Android.Net.Uri GetShareableUri(string file, FileProviderLocation location) { try { // use the specific location FileProvider.TemporaryLocation = location; // get the uri return(Platform.GetShareableFileUri(file)); } finally { // reset the location FileProvider.TemporaryLocation = FileProviderLocation.PreferExternal; } }
public void Get_Existing_External_Cache_Shareable_Uri(FileProviderLocation location) { // Save an external cache directory file var file = CreateFile(Platform.AppContext.ExternalCacheDir.AbsolutePath); // Make sure it is where we expect it to be Assert.True(FileProvider.IsFileInPublicLocation(file)); // Actually get a safe shareable file uri var shareableUri = GetShareableUri(file, location); // Make sure the uri is what we expected Assert.NotNull(shareableUri); Assert.Equal("content", shareableUri.Scheme); Assert.Equal("com.microsoft.maui.essentials.devicetests.fileProvider", shareableUri.Authority); Assert.Equal(new[] { "external_cache", Path.GetFileName(file) }, shareableUri.PathSegments); }
public void Get_Shareable_Uri(bool failAccess, FileProviderLocation location) { // Always fail to simulate unmounted media FileProvider.AlwaysFailExternalMediaAccess = failAccess; try { // Save a local cache data directory file var file = CreateFile(FileSystem.AppDataDirectory); // Make sure it is where we expect it to be Assert.False(FileProvider.IsFileInPublicLocation(file)); // Actually get a safe shareable file uri var shareableUri = GetShareableUri(file, location); // Determine where the file should be found var isInternal = failAccess || location == FileProviderLocation.Internal; var expectedCache = isInternal ? "internal_cache" : "external_cache"; var expectedCacheDir = isInternal ? Platform.AppContext.CacheDir.AbsolutePath : Platform.AppContext.ExternalCacheDir.AbsolutePath; // Make sure the uri is what we expected Assert.NotNull(shareableUri); Assert.Equal("content", shareableUri.Scheme); Assert.Equal("com.microsoft.maui.essentials.devicetests.fileProvider", shareableUri.Authority); Assert.Equal(4, shareableUri.PathSegments.Count); Assert.Equal(expectedCache, shareableUri.PathSegments[0]); Assert.Equal("2203693cc04e0be7f4f024d5f9499e13", shareableUri.PathSegments[1]); Assert.True(Guid.TryParseExact(shareableUri.PathSegments[2], "N", out var guid)); Assert.Equal(Path.GetFileName(file), shareableUri.PathSegments[3]); // Make sure the underlying file exists var realPath = Path.Combine(shareableUri.PathSegments.ToArray()) .Replace(expectedCache, expectedCacheDir, StringComparison.Ordinal); Assert.True(File.Exists(realPath)); } finally { FileProvider.AlwaysFailExternalMediaAccess = false; } }