简体   繁体   中英

Upload file from Android application to Microsoft OneDrive with Xamarin and C#

How can I upload file to OneDrive (SkyDrive) with Xamarin for Android?

I have information about Downloading and uploading files on OneDrive (Android)

Can I use Microsoft.Live in Xamarin Studio? I use it in Visual Studio for Windows Phone app:

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:

 <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? PS I can't use Visual Studio to create Android app, only Xamarin Studio.

You can use OneDrive for Android by creating and referencing Xamarin binding library. It allows you to create a binding project that automatically wraps the .jar library with C# wrappers based on a declarative approach.

Here is official manual

And here is a link to Xamarin forums with existing binding library project and examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM