static async Task <FileResult> PlatformCaptureAsync(MediaPickerOptions options, bool photo) { await Permissions.EnsureGrantedAsync <Permissions.Camera>(); await Permissions.EnsureGrantedAsync <Permissions.StorageWrite>(); var capturePhotoIntent = new Intent(photo ? MediaStore.ActionImageCapture : MediaStore.ActionVideoCapture); if (!Platform.IsIntentSupported(capturePhotoIntent)) { throw new FeatureNotSupportedException($"Either there was no camera on the device or '{capturePhotoIntent.Action}' was not added to the <queries> element in the app's manifest file. See more: https://developer.android.com/about/versions/11/privacy/package-visibility"); } capturePhotoIntent.AddFlags(ActivityFlags.GrantReadUriPermission); capturePhotoIntent.AddFlags(ActivityFlags.GrantWriteUriPermission); try { var activity = Platform.GetCurrentActivity(true); // Create the temporary file var ext = photo ? FileSystem.Extensions.Jpg : FileSystem.Extensions.Mp4; var fileName = Guid.NewGuid().ToString("N") + ext; var tmpFile = FileSystem.GetEssentialsTemporaryFile(Platform.AppContext.CacheDir, fileName); // Set up the content:// uri AndroidUri outputUri = null; void OnCreate(Intent intent) { // Android requires that using a file provider to get a content:// uri for a file to be called // from within the context of the actual activity which may share that uri with another intent // it launches. outputUri ??= FileProvider.GetUriForFile(tmpFile); intent.PutExtra(MediaStore.ExtraOutput, outputUri); } // Start the capture process await IntermediateActivity.StartAsync(capturePhotoIntent, Platform.requestCodeMediaCapture, OnCreate); // Return the file that we just captured return(new FileResult(tmpFile.AbsolutePath)); } catch (OperationCanceledException) { return(null); } }
internal static AndroidUri GetShareableFileUri(FileBase file) { Java.IO.File sharedFile; if (FileProvider.IsFileInPublicLocation(file.FullPath)) { // we are sharing a file in a "shared/public" location sharedFile = new Java.IO.File(file.FullPath); } else { var rootDir = FileProvider.GetTemporaryDirectory(); // create a unique directory just in case there are multiple file with the same name var tmpDir = new Java.IO.File(rootDir, Guid.NewGuid().ToString("N")); tmpDir.Mkdirs(); tmpDir.DeleteOnExit(); // create the new temprary file var tmpFile = new Java.IO.File(tmpDir, file.FileName); tmpFile.DeleteOnExit(); var fileUri = AndroidUri.Parse(file.FullPath); if (fileUri.Scheme == "content") { using var stream = Application.Context.ContentResolver.OpenInputStream(fileUri); using var destStream = System.IO.File.Create(tmpFile.CanonicalPath); stream.CopyTo(destStream); } else { System.IO.File.Copy(file.FullPath, tmpFile.CanonicalPath); } sharedFile = tmpFile; } // create the uri, if N use file provider if (HasApiLevelN) { var providerAuthority = AppContext.PackageName + ".fileProvider"; return(FileProvider.GetUriForFile( AppContext.ApplicationContext, providerAuthority, sharedFile)); } // use the shared file path created return(AndroidUri.FromFile(sharedFile)); }
internal static AndroidUri GetShareableFileUri(string filename) { Java.IO.File sharedFile; if (FileProvider.IsFileInPublicLocation(filename)) { // we are sharing a file in a "shared/public" location sharedFile = new Java.IO.File(filename); } else { var rootDir = FileProvider.GetTemporaryDirectory(); // create a unique directory just in case there are multiple file with the same name var tmpDir = new Java.IO.File(rootDir, Guid.NewGuid().ToString("N")); tmpDir.Mkdirs(); tmpDir.DeleteOnExit(); // create the new temprary file var tmpFile = new Java.IO.File(tmpDir, System.IO.Path.GetFileName(filename)); System.IO.File.Copy(filename, tmpFile.CanonicalPath); tmpFile.DeleteOnExit(); sharedFile = tmpFile; } // create the uri, if N use file provider if (HasApiLevelN) { var providerAuthority = AppContext.PackageName + ".fileProvider"; return(FileProvider.GetUriForFile( AppContext.ApplicationContext, providerAuthority, sharedFile)); } // use the shared file path created return(AndroidUri.FromFile(sharedFile)); }
internal static AndroidUri GetUriForFile(Java.IO.File file) => FileProvider.GetUriForFile(Platform.AppContext, Authority, file);