//private Task<FileData> TakeMediaAsync (string type, string action) private Task <FileData> TakeMediaAsync(string[] allowedTypes, string action) { var id = GetRequestId(); var ntcs = new TaskCompletionSource <FileData> (id); if (Interlocked.CompareExchange(ref _completionSource, ntcs, null) != null) { throw new InvalidOperationException("Only one operation can be active at a time"); } try { var pickerIntent = new Intent(this._context, typeof(FilePickerActivity)); pickerIntent.SetFlags(ActivityFlags.NewTask); pickerIntent.PutExtra(nameof(allowedTypes), allowedTypes); this._context.StartActivity(pickerIntent); EventHandler <FilePickerEventArgs> handler = null; EventHandler <EventArgs> cancelledHandler = null; handler = (s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePicked -= handler; tcs?.SetResult(new FileData(e.FilePath, e.FileName, () => { if (IOUtil.isMediaStore(e.FilePath)) { return(new System.IO.MemoryStream(e.FileByte)); } else { return(System.IO.File.OpenRead(e.FilePath)); } })); }; cancelledHandler = (s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePickCancelled -= cancelledHandler; tcs?.SetResult(null); }; FilePickerActivity.FilePickCancelled += cancelledHandler; FilePickerActivity.FilePicked += handler; } catch (Exception exAct) { Debug.Write(exAct); } return(_completionSource.Task); }
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Canceled) { // Notify user file picking was cancelled. OnFilePickCancelled(); Finish(); } else { System.Diagnostics.Debug.Write(data.Data); try { var _uri = data.Data; var filePath = IOUtil.getPath(context, _uri); if (string.IsNullOrEmpty(filePath)) { filePath = IOUtil.isMediaStore(_uri.Scheme) ? _uri.ToString() : _uri.Path; } byte[] file; if (IOUtil.isMediaStore(_uri.Scheme)) { file = IOUtil.readFile(context, _uri); } else { file = IOUtil.readFile(filePath); } var fileName = GetFileName(context, _uri); OnFilePicked(new FilePickerEventArgs(file, fileName, filePath)); } catch (Exception readEx) { System.Diagnostics.Debug.Write(readEx); // Notify user file picking failed. FilePickCancelled?.Invoke( this, new FilePickerCancelledEventArgs { Exception = readEx }); } finally { Finish(); } } }
//private Task<FileData> TakeMediaAsync (string type, string action) private Task <FileData> TakeMediaAsync(string[] allowedTypes, string action) { if (this._context.PackageManager.CheckPermission( Android.Manifest.Permission.ReadExternalStorage, this._context.PackageName) == Android.Content.PM.Permission.Denied) { throw new InvalidOperationException("Android permission READ_EXTERNAL_STORAGE is missing or was denied by user"); } var id = GetRequestId(); var ntcs = new TaskCompletionSource <FileData> (id); if (Interlocked.CompareExchange(ref _completionSource, ntcs, null) != null) { throw new InvalidOperationException("Only one operation can be active at a time"); } try { var pickerIntent = new Intent(this._context, typeof(FilePickerActivity)); pickerIntent.SetFlags(ActivityFlags.NewTask); pickerIntent.PutExtra(nameof(allowedTypes), allowedTypes); this._context.StartActivity(pickerIntent); EventHandler <FilePickerEventArgs> handler = null; EventHandler <FilePickerCancelledEventArgs> cancelledHandler = null; handler = (s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePickCancelled -= cancelledHandler; FilePickerActivity.FilePicked -= handler; tcs?.SetResult(new FileData(e.FilePath, e.FileName, () => { if (IOUtil.isMediaStore(e.FilePath)) { var contentUri = Android.Net.Uri.Parse(e.FilePath); return(Application.Context.ContentResolver.OpenInputStream(contentUri)); } else { return(System.IO.File.OpenRead(e.FilePath)); } })); }; cancelledHandler = (s, e) => { var tcs = Interlocked.Exchange(ref _completionSource, null); FilePickerActivity.FilePickCancelled -= cancelledHandler; FilePickerActivity.FilePicked -= handler; if (e?.Exception != null) { tcs?.SetException(e.Exception); } else { tcs?.SetResult(null); } }; FilePickerActivity.FilePickCancelled += cancelledHandler; FilePickerActivity.FilePicked += handler; } catch (Exception exAct) { Debug.Write(exAct); _completionSource.SetException(exAct); } return(_completionSource.Task); }