diff --git a/src/Media.Plugin/Android/MediaImplementation.cs b/src/Media.Plugin/Android/MediaImplementation.cs index 0ae5b24a..65563dac 100644 --- a/src/Media.Plugin/Android/MediaImplementation.cs +++ b/src/Media.Plugin/Android/MediaImplementation.cs @@ -189,7 +189,6 @@ async Task FixOrientationAndResize(PickMediaOptions options, MediaFile media) throw new MediaPermissionException(nameof(Permissions.Camera)); } - VerifyOptions(options); var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options, token); @@ -199,54 +198,34 @@ async Task FixOrientationAndResize(PickMediaOptions options, MediaFile media) if (options.SaveToAlbum) { + var fileName = System.IO.Path.GetFileName(media.Path); + var uri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true); + var f = new Java.IO.File(uri.Path); + try { - var fileName = System.IO.Path.GetFileName(media.Path); - var publicUri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true); - using (System.IO.Stream input = File.OpenRead(media.Path)) - using (System.IO.Stream output = File.Create(publicUri.Path)) - input.CopyTo(output); - - media.AlbumPath = publicUri.Path; + var values = new ContentValues(); + values.Put(MediaStore.Audio.Media.InterfaceConsts.DisplayName, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath)); + values.Put(MediaStore.Audio.Media.InterfaceConsts.MimeType, "image/jpeg"); + values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty); + values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis()); + values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode()); + values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant()); - var f = new Java.IO.File(publicUri.Path); + var cr = context.ContentResolver; + var albumUri = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values); - //MediaStore.Images.Media.InsertImage(context.ContentResolver, - // f.AbsolutePath, f.Name, null); - - try - { - MediaScannerConnection.ScanFile(context, new[] { f.AbsolutePath }, null, context as MediaPickerActivity); - - var values = new ContentValues(); - values.Put(MediaStore.Images.Media.InterfaceConsts.Title, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath)); - values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty); - values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis()); - values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode()); - values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant()); - values.Put("_data", f.AbsolutePath); - - var cr = context.ContentResolver; - cr.Insert(MediaStore.Images.Media.ExternalContentUri, values); - } - catch (Exception ex1) - { - Console.WriteLine("Unable to save to scan file: " + ex1); - } - - var contentUri = Android.Net.Uri.FromFile(f); - var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile, contentUri); - context.SendBroadcast(mediaScanIntent); + using (System.IO.Stream input = File.OpenRead(media.Path)) + using (System.IO.Stream output = cr.OpenOutputStream(albumUri)) + input.CopyTo(output); } - catch (Exception ex2) + catch (Exception ex) { - Console.WriteLine("Unable to save to gallery: " + ex2); + Console.WriteLine("Unable to save to gallery: " + ex); } } //check to see if we need to rotate if success - - try { var exif = new ExifInterface(media.Path); diff --git a/src/Media.Plugin/Android/MediaPickerActivity.cs b/src/Media.Plugin/Android/MediaPickerActivity.cs index f227db8a..f281142c 100644 --- a/src/Media.Plugin/Android/MediaPickerActivity.cs +++ b/src/Media.Plugin/Android/MediaPickerActivity.cs @@ -499,18 +499,19 @@ static string GetUniquePath(string folder, string name, bool isPhoto) return Path.Combine(folder, nname); } - /// - /// Try go get output file - /// - /// - /// - /// - /// - /// - /// + /// + /// Try go get output file + /// + /// + /// + /// + /// + /// + /// public static Uri GetOutputMediaFile(Context context, string subdir, string name, bool isPhoto, bool saveToAlbum) { subdir = subdir ?? string.Empty; + Uri uri; if (string.IsNullOrWhiteSpace(name)) { @@ -521,14 +522,37 @@ public static Uri GetOutputMediaFile(Context context, string subdir, string name name = "VID_" + timestamp + ".mp4"; } - var mediaType = (isPhoto) ? Environment.DirectoryPictures : Environment.DirectoryMovies; - var directory = saveToAlbum ? Environment.GetExternalStoragePublicDirectory(mediaType) : context.GetExternalFilesDir(mediaType); - using (var mediaStorageDir = new Java.IO.File(directory, subdir)) + if ((int)Build.VERSION.SdkInt < 29) { - if (!mediaStorageDir.Exists()) + var mediaType = (isPhoto) ? Environment.DirectoryPictures : Environment.DirectoryMovies; + var directory = saveToAlbum ? Environment.GetExternalStoragePublicDirectory(mediaType) : context.GetExternalFilesDir(mediaType); + + using (var mediaStorageDir = new Java.IO.File(directory, subdir)) { - if (!mediaStorageDir.Mkdirs()) - throw new IOException("Couldn't create directory, have you added the WRITE_EXTERNAL_STORAGE permission?"); + if (!mediaStorageDir.Exists()) + { + if (!mediaStorageDir.Mkdirs()) + throw new IOException("Couldn't create directory, have you added the WRITE_EXTERNAL_STORAGE permission?"); + + if (!saveToAlbum) + { + // Ensure this media doesn't show up in gallery apps + using (var nomedia = new Java.IO.File(mediaStorageDir, ".nomedia")) + nomedia.CreateNewFile(); + } + } + + uri = Uri.FromFile(new Java.IO.File(GetUniquePath(mediaStorageDir.Path, name, isPhoto))); + } + } + else + { + var mediaType = (isPhoto) ? Environment.DirectoryPictures : Environment.DirectoryMovies; + var directory = context.GetExternalFilesDir(mediaType); + + using (var mediaStorageDir = new Java.IO.File(directory, subdir)) + { + mediaStorageDir.Mkdirs(); if (!saveToAlbum) { @@ -536,10 +560,18 @@ public static Uri GetOutputMediaFile(Context context, string subdir, string name using (var nomedia = new Java.IO.File(mediaStorageDir, ".nomedia")) nomedia.CreateNewFile(); } + + uri = Uri.FromFile(new Java.IO.File(GetUniquePath(mediaStorageDir.Path, name, isPhoto))); } - return Uri.FromFile(new Java.IO.File(GetUniquePath(mediaStorageDir.Path, name, isPhoto))); + if (saveToAlbum) + { + uri = (isPhoto) ? MediaStore.Images.Media.ExternalContentUri : MediaStore.Video.Media.ExternalContentUri; + uri = Uri.WithAppendedPath(uri, name); + } } + + return uri; } internal static Task> GetFileForUriAsync(Context context, Uri uri, bool isPhoto, bool saveToAlbum)