private async Task DoBackup(Uri uri, string password) { var backup = new Backup( _authSource.GetAll(), _categorySource.GetAll(), _authSource.CategoryBindings, _customIconSource.GetAll() ); var dataToWrite = backup.ToBytes(password); // This is the only way of reliably writing files using SAF on Xamarin. // A file output stream will usually create 0 byte files on virtual storage such as Google Drive var output = ContentResolver.OpenOutputStream(uri, "rwt"); var dataStream = new DataOutputStream(output); try { await dataStream.WriteAsync(dataToWrite); await dataStream.FlushAsync(); } finally { dataStream.Close(); output.Close(); } }
public static async Task WriteFile(Context context, Uri uri, byte[] data) { // Run backup on separate thread, file writing on the main thread fails when using Nextcloud await Task.Run(async delegate { // This is the only way of reliably writing binary files using SAF on Xamarin. // A file output stream will usually create 0 byte files on virtual storage such as Google Drive Stream output = null; DataOutputStream dataStream = null; try { output = context.ContentResolver.OpenOutputStream(uri); dataStream = new DataOutputStream(output); await dataStream.WriteAsync(data); await dataStream.FlushAsync(); } finally { dataStream?.Close(); output?.Close(); } }); }