/// <summary> /// Tries the move file asynchronous. /// </summary> /// <param name="context">The execution context.</param> /// <param name="url">The original file to move.</param> /// <param name="path">The file destination.</param> /// <param name="isPhoto">A value indicating whenter the operation is performed on a photo.</param> /// <returns>A <see cref="Task"/> represening the asyncroneous operation.</returns> private static Task <bool> TryMoveFileAsync(Context context, Uri url, Uri path, bool isPhoto) { var moveTo = MediaPickerActivity.GetLocalPath(path); return(MediaPickerActivity.GetFileForUriAsync(context, url, isPhoto).ContinueWith( t => { // If nothing to move if (t.Result.Item1 == null) { return false; } // Move the file to required destination File.Delete(moveTo); File.Move(t.Result.Item1, moveTo); // If the schema is content if (url.Scheme == "content") { // Delete content file context.ContentResolver.Delete(url, null, null); } return true; }, TaskScheduler.Default)); }
/// <summary> /// Gets the media file asynchronous. /// </summary> /// <param name="context">The execution context.</param> /// <param name="requestCode">The request operation code.</param> /// <param name="action">The action to perform.</param> /// <param name="isPhoto">A value indicating whenter the operation is performed on a photo.</param> /// <param name="path">The media path.</param> /// <param name="data">The media data URI.</param> /// <returns>A <see cref="Task"/> represening the asyncroneous operation.</returns> private static Task <MediaPickedEventArgs> GetMediaFileAsync( Context context, int requestCode, string action, bool isPhoto, ref Uri path, Uri data) { // If requested to take a photo Task <Tuple <string, bool> > pathFuture; Action <bool> dispose = null; string originalPath = null; if (action != Intent.ActionPick) { originalPath = path.Path; // Not all camera apps respect EXTRA_OUTPUT, some will instead return a content or file uri from data. if ((data != null) && (data.Path != originalPath)) { // Move the camera output originalPath = data.ToString(); var currentPath = path.Path; pathFuture = MediaPickerActivity.TryMoveFileAsync(context, data, path, isPhoto) .ContinueWith(t => new Tuple <string, bool>(t.Result ? currentPath : null, false)); } else { pathFuture = MediaPickerActivity.TaskFromResult(new Tuple <string, bool>(path.Path, false)); } } else if (data != null) { // Pick the file from media galery originalPath = data.ToString(); path = data; pathFuture = MediaPickerActivity.GetFileForUriAsync(context, path, isPhoto); } else { // Nothing to perform pathFuture = MediaPickerActivity.TaskFromResult <Tuple <string, bool> >(null); } return(pathFuture.ContinueWith( t => { // If selected file exists var resultPath = t.Result.Item1; if ((resultPath != null) && File.Exists(t.Result.Item1)) { // Return the selected media if (t.Result.Item2) { dispose = d => File.Delete(resultPath); } var mf = new MediaFile(resultPath, () => File.OpenRead(t.Result.Item1), dispose); return new MediaPickedEventArgs(requestCode, false, mf); } // Return an error return new MediaPickedEventArgs( requestCode, new FileNotFoundException("Media file not found", originalPath)); })); }