OneDrive picker and saver for Android
Last modified: October 20, 2014
Applies to: Office 365 | OneDrive
The OneDrive picker for Android is the fastest way to get files from OneDrive and OneDrive for Business into your Android app. It provides a set of Java APIs that you can use in your app to download your user’s OneDrive files, or get view links to those files, all without handling authentication. The OneDrive picker also lets your app get thumbnails of selected image and video files from OneDrive, so that you don’t have to build them yourself.
The OneDrive saver for Android provides another set of APIs, in the same SDK, that you can use in your app to let your users save files on their device to their OneDrive or OneDrive for Business.
In this article, we’ll show you how to get your app quickly opening files from and saving files to OneDrive, and how to use all of the features of the picker and the saver. You can follow along by running our picker sample app and our saver sample app
The OneDrive picker and saver for Android are available as open source on GitHub. You can either build the library yourself, or download the latest version.
For information on configuring your environment to work with the sample apps for the picker and saver, see Configuring your Android environment.
Register your apps and get app IDs (client IDs) to use the SDK.
Building the library
Eclipse
-
In Eclipse, go to File → Import → General → Existing Projects into Workspace.
-
Click Browse.. to select the onedrive-picker-android, where you saved the SDK, as your root directory. Make sure OneDriveSDK is checked.
-
From the Android SDK Manager, install Android 4.4.2 (API 19), if not already installed.
-
Right-click your project and select Properties and go to Android in the left side bar.
-
Click Add.. in Library and select OneDriveSDK to link it to your project.
Android Studio
-
Choose Import Project... or Import Module..., to import into an existing project.
-
Browse to the location where the SDK is saved and select the root onedrive-picker-android.
-
If the Android 4.4.2 (API 19) build is not already installed, follow prompts or go to the Android SDK Manager to install it and associated build tools.
Your app needs to give users the ability to open files from OneDrive. The picker does this by generating links to users’ files. The LinkType enumeration enumerates the two different kinds of links that the picker can return:
-
LinkType.DownloadLink -- A URL is returned that provides access for one hour directly to the contents of the file. You can use this URL to download the file into your application.
-
LinkType.WebViewLink -- A sharing link is created that provides a web preview of the file. The link is valid until the user deletes the shared link through OneDrive. Sharing links are not available for OneDrive for Business files.
This example sets up a click handler that launches the picker from onClick. When the onClick method is called, the picker is created and configured for the type of link requested by the user. Then, the IPicker.startPicking method method launches the picking experience. In this case, the app is requesting a view-only link type by using LinkType.WebViewLink.
import com.microsoft.onedrivesdk.picker.*; private IPicker mPicker; //Replace <app_id> with your app’s ID private string ONEDRIVE_APP_ID = "<app_id>"; // The onClickListener that will start the OneDrive picker private final OnClickListener mStartPickingListener = new OnClickListener() { @Override public void onClick(final View v) { mPicker = Picker.createPicker(ONEDRIVE_APP_ID) mPicker.startPicking((Activity)v.getContext(), LinkType.WebViewLink); } };
Catching the results
When the user has completed opening a file or has canceled from the picker, call the onActivityResult method to handle the picker results. In this method you can catch the results and get access to the file that was selected by the user. If the user does not pick a file, the IPickerResult interface object result will be null.
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { // Handle the OneDrive picker response if (requestCode == ONEDRIVE_PICKER_REQUEST) { // Get the results from the picker IPickerResult result = mPicker.getPickerResult(requestCode, resultCode, data); // Handle the case if nothing was picked if (result == null) { Toast.makeText(this, "Did not get a file from the picker!", Toast.LENGTH_LONG).show(); return; } // Do something with the picked file Log.d("main", "Link to file '" + result.getName() + ": " + result.getLink()); } else { // Handle the non-OneDrive picker request } }
Result object
In addition to the filename and link for the file, you can access several other properties of the result returned by the picker.
public static class IFileResult { // Name of the file, including the file extension public string getName(); // Type of link generated public LinkType getLinkType(); // URI for the file, which varies based on the value of getLinkType() public Uri getLink(); // Size of the file, in bytes public int getSize(); // Set of thumbnail links for various sizes: "small", "medium", and "large" public Map<string, Uri> getThumnailLinks(); }
Your app can also provide a way for users to save files to OneDrive and OneDrive for Business. Like the picker, the saver also launches a OneDrive experience, the saver experience, in which the user can choose a location to save the file provided by your app.
Sample code
In the following code sample, a click handler is used to launch the saver from the onClick method. Your app should have a filename and a URI that points to the file on the device to pass to the saver. As an example, this sample code creates a placeholder file named file.txt in the app’s local folder that gets passed to the saver.
Note
|
|---|
|
Be sure to replace APP_ID in the code with your application’s identifier. |
import com.microsoft.onedrivesdk.saver.* private ISaver mSaver; private String ONEDRIVE_APP_ID = "APP_ID"; // Get app id here: https://account.live.com/developers/applications // The onClickListener that will start the OneDrive picker private final OnClickListener mStartPickingListener = new OnClickListener() { @Override public void onClick(final View v) { // create example file to save to OneDrive final String filename = "file.txt"; final File f = new File(context.getFilesDir(), filename); // create and launch the saver mSaver = Saver.createSaver(ONEDRIVE_APP_ID); mSaver.startSaving((Activity)v.getContext(), filename, Uri.fromFile(f)); } };
When the onClick method is called, the saver is created, and then the Saver.startSaving method is called. This launches the OneDrive saver experience, allowing your users to pick a folder to upload the file. If the user does not have the OneDrive app installed when the startSaving method is called, they will be prompted to download the app from the marketplace.
The saver currently supports the file://<path>/<filename>and the Android content://<path>/<filename> URI schemes. If a different URI scheme is used, the saver will return a NoFileSpecified error. See the next section for details about the saver response.
Saver result
When the user has finished saving a file, or if there was a problem saving, the Android onActivityResult method is called to handle the saver result. By using this method, you can check to see if the file was saved, and if it wasn’t, you can catch the exception and handle the error.
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
// check that the file was successfully saved to OneDrive
try {
mSaver.handleSave(requestCode, resultCode, data);
} catch (final SaverException e) {
// Log error information
Log.e("OneDriveSaver". e.getErrorType().toString()); // Provides one of the SaverError enum
Log.d("OneDriveSaver", e.getDebugErrorInfo()); // Detailed debug error message
}
}
The error message provided by the SaverException.getDebugErrorInfo method is primarily for development and debugging and can change at any time. When handling errors, you can use the SaverException.getErrorType method to determine the general cause of the error.
Saver error types
When the saver is unable to complete saving a file and throws an exception, you can use the getErrorType method to get a SaverError enumeration constant that indicates one of a set of possible error types.
-
The OneDrive picker library is supported for Android API revision 14 "Ice Cream Sandwich" and greater.
-
The OneDrive Android app must be installed in order for the picker and saver to function. If the OneDrive app is not installed, the user will be prompted to download the app when the startPicking or startSaving method is called.
-
When used in the Android emulator, the OneDrive picker and saver display an error message because neither the OneDrive app nor the app store is available. You need to deploy your app to a device to test the OneDrive picker and saver integration.
Note