private async Task CreateCopy(Uri sourceUri, Uri destinationUri) { using (var source = new FileInputStream(ContentResolver.OpenFileDescriptor(sourceUri, "r").FileDescriptor)) { using (var destination = new FileOutputStream(ContentResolver.OpenFileDescriptor(destinationUri, "w").FileDescriptor)) { byte[] content = new byte[source.Available()]; await source.ReadAsync(content); await destination.WriteAsync(content); destination.Close(); } } }
protected async override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Canceled && requestCode == 43) { // Notify user file picking was cancelled. OnDirectoryPickCancelled(); Finish(); } else { System.Diagnostics.Debug.Write(data.Data); try { // read the file out of the cache dir var file = new Java.IO.File(filePath); var fileLength = (int)file.Length(); byte[] fileBytes = new byte[fileLength]; using (var inputStream = new FileInputStream(filePath)) { await inputStream.ReadAsync(fileBytes, 0, fileLength); } // write the file to the path chosen by the user using (var pFD = ContentResolver.OpenFileDescriptor(data.Data, "w")) using (var outputSteam = new FileOutputStream(pFD.FileDescriptor)) { outputSteam.Write(fileBytes); } OnDirectoryPicked(new SavePickerEventArgs(data.Data)); } catch (Exception readEx) { // Notify user file picking failed. OnDirectoryPickError(readEx.Message); System.Diagnostics.Debug.Write(readEx); } finally { Finish(); } } }