[英]Upload file from Android application to Microsoft OneDrive with Xamarin and C#
How can I upload file to OneDrive (SkyDrive) with Xamarin for Android? 如何使用Xamarin for Android将文件上传到OneDrive(SkyDrive)?
I have information about Downloading and uploading files on OneDrive (Android) 我了解有关在OneDrive(Android)上下载和上传文件的信息
Can I use Microsoft.Live in Xamarin Studio? 我可以在Xamarin Studio中使用Microsoft.Live吗? I use it in Visual Studio for Windows Phone app:
我在Visual Studio for Windows Phone应用程序中使用它:
C#: C#:
private void skydrive_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
if (e != null && e.Status == LiveConnectSessionStatus.Connected)
{
this.client = new LiveConnectClient(e.Session);
this.GetAccountInformations();
}
else
{
this.client = null;
InfoText.Text = e.Error != null ? e.Error.ToString() : string.Empty;
}
}
private async void GetAccountInformations()
{
try
{
LiveOperationResult operationResult = await this.client.GetAsync("me");
var jsonResult = operationResult.Result as dynamic;
string firstName = jsonResult.first_name ?? string.Empty;
string lastName = jsonResult.last_name ?? string.Empty;
InfoText.Text = "Welcome " + firstName + " " + lastName;
}
catch (Exception e)
{
InfoText.Text = e.ToString();
}
}
private async void btnUpload_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
try
{
LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive",
new Uri("/shared/transfers/" + fileName, UriKind.Relative),
OverwriteOption.Overwrite
);
InfoText.Text = "File " + fileName + " uploaded";
}
catch (Exception ex)
{
}
}
}
}
XAML: XAML:
<Controls:SignInButton Canvas.Left="10" Canvas.Top="351" Content="Button"
Name="skydrive" Scopes="wl.basic wl.signin wl.offline_access wl.skydrive_update"
SessionChanged="skydrive_SessionChanged"
ClientId="00000000########"/>
<TextBlock Name="InfoText" Width="167" Height="42" Canvas.Left="192" Canvas.Top="367"></TextBlock>
<Button Name="btnUpload" Canvas.Left="10" Canvas.Top="430" Width="166" Click="btnUpload_Click">Upload</Button>
Are there other ways to upload file from Android app to other server? 还有其他方法可以将文件从Android应用上传到其他服务器吗? PS I can't use Visual Studio to create Android app, only Xamarin Studio.
PS我不能使用Visual Studio创建Android应用程序,只能使用Xamarin Studio。
You can use OneDrive for Android by creating and referencing Xamarin binding library. 您可以通过创建和引用Xamarin绑定库来使用OneDrive for Android。 It allows you to create a binding project that automatically wraps the .jar library with C# wrappers based on a declarative approach.
它允许您创建一个绑定项目,该项目基于声明性方法使用C#包装器自动包装.jar库。
Here is official manual 这是官方手册
And here is a link to Xamarin forums with existing binding library project and examples 这是带有现有绑定库项目和示例的Xamarin论坛的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.