Downloading and uploading files on OneDrive (Android)

Your Android apps can use the Live SDK for Android to download or upload files on Microsoft OneDrive.

In this article
Prerequisites
Upload a file
Download a file
Update files

Prerequisites

We assume that the user has already signed in and consented to the wl.skydrive scope for reading file or folder properties. If you have not added user sign-in to your Android apps, see Signing users in (Android).

We assume that you are familiar with Java, and can write Android apps.

Upload a file

To upload a file, photo, video, or audio, use code like this. You can upload file sizes up to 100 megabytes. In this example, the uploadAsync is called with the path, file name, and LiveUploadOperationListener. When the upload is successful, onUploadCompleted is called to display a message to the user that the file was uploaded.

Note

Before uploading a file, have your app check to make sure that there is enough available OneDrive storage space. For example, your app could get the size of the file in bytes and then compare it to the number of available storage bytes returned in the OneDrive quota call. For more info, see Get a user's total and remaining OneDrive storage quota.

public void createFile() {
	final Runnable uploadImage = new Runnable() {
		public void run() {
			final String imageUrl = "http://i.imgur.com/LYZEGB.jpg"; 
			final String fileName = "Vase-Cup.jpg";
			final URL url;
			final URLConnection ucon;
			final InputStream is;
			try {
				url = new URL(imageUrl); 
				ucon = url.openConnection();
				is = ucon.getInputStream();
				client.uploadAsync("me/skydrive", fileName, is, new LiveUploadOperationListener() {
					public void onUploadFailed(LiveOperationException exception, LiveOperation operation) {
						resultTextView.setText("Error uploading file: " + exception.getMessage());
					}
					public void onUploadCompleted(LiveOperation operation) {
						resultTextView.setText("File uploaded.");
						try {
							is.close();
						}    
						catch(IOException ioe) {
							resultTextView.setText("Error uploading file: " + ioe.getMessage());
						}
					}
					public void onUploadProgress(int totalBytes, int bytesRemaining, LiveOperation operation)
						  {
							  resultTextView.setText("Uploading file... " + bytesRemaining + " bytes remaining " +
								"(" + (bytesRemaining / totalBytes) * 100 + "%)");
						  }
				});
			}
			catch(IOException ioex) {
				resultTextView.setText("Error uploading file: " + ioex.getMessage());
				return;
			}
			catch(Exception ex)
			{
				resultTextView.setText("Error uploading file: " + ex.getMessage());
				return;
			} 
		}
	};
	auth.login(this, Arrays.asList(new String[] { "wl.skydrive_update" }), new LiveAuthListener() {
		public void onAuthError(LiveAuthException exception, Object userState) {
			resultTextView.setText("Error signing in: " + exception.getMessage());
		}
		  public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
			  new Thread(uploadImage).start();
		}
	  });   
	
}

Download a file

Users can get the contents of a file, photo, video, or audio. The wl.skydrive scope is required. In this example, the downloadAsync method is called with a file, and a new LiveDownloadOperationListener object as parameters. When the onDownloadCompleted method is called, a text is displayed, notifying the user that the download was successful.

Note

You can instruct the user's web browser to prompt the user to save the file that's downloading instead of having the browser try to download and display the file directly in its own window. To do this, add the download=true query-string parameter after /content (for example, FILE_ID/content?download=true). This call adds the Content-Disposition: attachment header to the response.

public void downloadFile() {
	String file = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/picture?type=thumbnail";
	client.downloadAsync(file, new LiveDownloadOperationListener() {
		public void onDownloadCompleted(LiveDownloadOperation operation)
		{
			try {
				resultTextView.setText("Picture downloaded.");
				InputStream input =  operation.getStream();
				Bitmap bMap = BitmapFactory.decodeStream(input);
				picture.setImageBitmap(bMap);
				input.close();                    
			}
			catch(java.io.IOException ex) {
				resultTextView.setText("Error downloading picture: " + ex.getMessage());
			}
		}
		public void onDownloadFailed(LiveOperationException exception, LiveDownloadOperation operation)
		{
			resultTextView.setText(exception.getMessage());
		}
		public void onDownloadProgress(int totalBytes, int bytesRemaining, LiveDownloadOperation operation)
		{
			resultTextView.setText("Downloading picture... " + bytesRemaining + " bytes downloaded " +
				"(" + (bytesRemaining / totalBytes) * 100 + "%)");
		}
	});
}

Update files

To change contents for an existing file, photo, video, or audio, use the code in the preceding Upload a file section. Call the LiveConnectClient.uploadAsync method with the OverwriteOption.Overwrite enumeration assigned to the OverwriteOption value.