/// <summary> /// /// </summary> /// <param name="self"></param> /// <param name="rootPath"></param> /// <returns></returns> public static string GetFilePath(this StoreMediaOptions self, string rootPath) { var isPhoto = !(self is StoreVideoOptions); var name = (self != null) ? self.Name : null; if (String.IsNullOrWhiteSpace(name)) { var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture); if (isPhoto) { name = "IMG_" + timestamp + ".jpg"; } else { name = "VID_" + timestamp + ".mp4"; } } var ext = Path.GetExtension(name); if (ext == String.Empty) { ext = ((isPhoto) ? ".jpg" : ".mp4"); } name = Path.GetFileNameWithoutExtension(name); var folder = Path.Combine(rootPath ?? String.Empty, (self != null && self.Directory != null) ? self.Directory : String.Empty); return(Path.Combine(folder, name + ext)); }
/// <summary> /// /// </summary> /// <param name="self"></param> /// <param name="rootPath"></param> /// <param name="checkExists"></param> /// <returns></returns> public static string GetUniqueFilepath(this StoreMediaOptions self, string rootPath, Func <string, bool> checkExists) { var path = self.GetFilePath(rootPath); var folder = Path.GetDirectoryName(path); var ext = Path.GetExtension(path); var name = Path.GetFileNameWithoutExtension(path); var nname = name + ext; var i = 1; while (checkExists(Path.Combine(folder, nname))) { nname = name + "_" + (i++) + ext; } return(Path.Combine(folder, nname)); }
/// <summary> /// /// </summary> /// <param name="self"></param> public static void VerifyOptions(this StoreMediaOptions self) { if (self == null) { throw new ArgumentNullException("options"); } //if (!Enum.IsDefined (typeof(MediaFileStoreLocation), options.Location)) // throw new ArgumentException ("options.Location is not a member of MediaFileStoreLocation"); //if (options.Location == MediaFileStoreLocation.Local) //{ //if (String.IsNullOrWhiteSpace (options.Directory)) // throw new ArgumentNullException ("options", "For local storage, options.Directory must be set"); if (Path.IsPathRooted(self.Directory)) { throw new ArgumentException("options.Directory must be a relative path", "options"); } //} }
private Task<Plugin.Media.Abstractions.MediaFile> TakeMediaAsync(string type, string action, StoreMediaOptions options) { int id = GetRequestId(); var ntcs = new TaskCompletionSource<Plugin.Media.Abstractions.MediaFile>(id); if (Interlocked.CompareExchange(ref this.completionSource, ntcs, null) != null) throw new InvalidOperationException("Only one operation can be active at a time"); this.context.StartActivity(CreateMediaIntent(id, type, action, options)); EventHandler<MediaPickedEventArgs> handler = null; handler = (s, e) => { var tcs = Interlocked.Exchange(ref this.completionSource, null); MediaPickerActivity.MediaPicked -= handler; if (e.RequestId != id) return; if (e.Error != null) tcs.SetResult(null); else if (e.IsCanceled) tcs.SetResult(null); else tcs.SetResult(e.Media); }; MediaPickerActivity.MediaPicked += handler; return completionSource.Task; }
private Intent CreateMediaIntent(int id, string type, string action, StoreMediaOptions options, bool tasked = true) { Intent pickerIntent = new Intent(this.context, typeof(MediaPickerActivity)); pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id); pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type); pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action); pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked); if (options != null) { pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory); pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name); var cameraOptions = (options as StoreCameraMediaOptions); if (cameraOptions != null) { if (cameraOptions.DefaultCamera == CameraDevice.Front) { pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", 1); } pickerIntent.PutExtra(MediaPickerActivity.ExtraSaveToAlbum, cameraOptions.SaveToAlbum); } var vidOptions = (options as StoreVideoOptions); if (vidOptions != null) { if (vidOptions.DefaultCamera == CameraDevice.Front) { pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", 1); } pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds); pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality); } } //pickerIntent.SetFlags(ActivityFlags.ClearTop); pickerIntent.SetFlags(ActivityFlags.NewTask); return pickerIntent; }
private void VerifyOptions(StoreMediaOptions options) { if (options == null) throw new ArgumentNullException("options"); if (System.IO.Path.IsPathRooted(options.Directory)) throw new ArgumentException("options.Directory must be a relative path", "options"); }
private Intent CreateMediaIntent(int id, string type, string action, StoreMediaOptions options, bool tasked = true) { Intent pickerIntent = new Intent(this.context, typeof(MediaPickerActivity)); pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id); pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type); pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action); pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked); if (options != null) { pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory); pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name); var vidOptions = (options as StoreVideoOptions); if (vidOptions != null) { pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds); pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality); } } //pickerIntent.SetFlags(ActivityFlags.ClearTop); pickerIntent.SetFlags(ActivityFlags.NewTask); return pickerIntent; }