Items in the OneDrive SDK behave just like items through the OneDrive API. All actions on items described in the OneDrive API are available through the SDK. For more information, see the Items Reference.
The examples in this topic all use a previously created oneDriveClient
object.
- Get an item
- Delete an item
- Upload a file
- Download a file
- Move an item
- Rename an item
- Copy an item
- Upload a large file
To get an item, you construct request builders getDrive
and getItems
, you call buildRequest
to build the request, and then make a final call to get
.
Name | Description |
---|---|
itemId | The item id of the item to retrieve. |
callback | The callback for when the get call is returned. |
//Enter the itemId here.
final String itemId = "0000000000000000000000";
final ICallback<Item> callback = new ICallback<Item>() {
@Override
public void success(final Item result) {
Toast.makeText(getActivity(), "Got item", Toast.LENGTH_LONG).show();
}
...
// Handle failure
}
oneDriveClient
.getDrive()
.getItems(itemId)
.buildRequest()
.get(callback);
To delete an item, you construct request builders to get the item you want to delete, and then call delete
on the item.
Name | Description |
---|---|
itemId | The item id of the item to delete. |
callback | The callback for when the get call is completed. |
final String itemId = "0000000000000000000000";
final ICallback<Void> callback = new ICallback<Void>() {
@Override
public void success(final Void ignored) {
Toast.makeText(getActivity(), "Deleted item", Toast.LENGTH_LONG).show();
}
...
// Handle failure
}
oneDriveClient
.getDrive()
.getItems(itemId)
.buildRequest()
.delete(callback);
To upload a file, you chain together build requests in this order: getDrive
, getItems
, getChildren
, byID
, and then getContent
. You then call buildRequest
to build the requests, and finally, put
to complete the upload.
Name | Description |
---|---|
filename | The name of the file to upload. |
fileContents | The byte array (byte[]) that contains the file contents. |
callback | The callback when the file is uploaded, as well as progress reported. |
final String filename = "The Filename.txt";
final byte[] fileContents = new byte[] { /* The File contents to upload */};
final IProgressCallback<Item> callback = new IProgressCallback<Item>() {
public void success(final Item item) {
Toast.makeText(getActivity(), "Item uploaded", Toast.LENGTH_LONG).show();
}
...
// Handle progress
// Handle failure
}
oneDriveClient
.getDrive()
.getItems(ItemId)
.getChildren()
.byId(filename)
.getContent()
.buildRequest()
.put(fileContents, callback);
To download a file, you construct the request builders getDrive
, getItems
, and getContent
, and then call buildRequest
to build the request. Finally, you call get
to retrieve the item to download.
Name | Description |
---|---|
itemId | The item id of the item to download. |
final String itemId = "0000000000000000000000";
final InputStream inputStream = oneDriveClient
.getDrive()
.getItems(itemId)
.getContent()
.buildRequest()
.get();
// Use the input stream
// Close the input stream
To move an item, construct request builders to get the item with getItem
, and then call update
with the new location.
Name | Description |
---|---|
newParentId | The new parent's item id. |
itemId | The item id of the item to move. |
newLocation | The specific aspects of the item to update. |
callback | The callback when the item update has returned. |
final String newParentId = "0000000000000000000000";
final String itemId = "0000000000000000000000";
final Item newLocation = new Item();
newLocation.parentReference = new ItemReference();
newLocation.parentReference.id = newParentId;
final ICallback<Item> callback = new ICallback<Item>() {
@Override
public void success(final Item result) {
Toast.makeText(getActivity(), "Update the item location", Toast.LENGTH_LONG).show();
}
...
// Handle failure
}
oneDriveClient
.getDrive()
.getItems(itemId)
.buildRequest()
.update(newLocation, callback);
Like all other operations, you rename an item by constructing request builders getDrive
and getItems
on the item, and then calling update
with the new name.
Name | Description |
---|---|
newItemName | The new name for the item. |
itemId | The item id of the item to rename. |
newName | The specific aspects of the item to update. |
callback | The callback when the item update has returned. |
final String newItemName = "My File.txt";
final String itemId = "0000000000000000000000";
final Item newName = new Item();
newName.name = newItemName;
final ICallback<Item> callback = new ICallback<Item>() {
@Override
public void success(final Item result) {
Toast.makeText(getActivity(), "Update the item name", Toast.LENGTH_LONG).show();
}
...
// Handle failure
}
oneDriveClient
.getDrive()
.getItems(itemId)
.buildRequest()
.update(newName, callback);
Copy requests are processed asynchronously on the service, so the pattern is slightly different, and can require multiple sets of requests.
You create a copy request for an item by constructing request builders getDrive
, getItems
and 'getCopy' on the item, and then calling create
to start the request.
Name | Description |
---|---|
itemId | The item id of the item to copy. |
copiedItemName | The name for the copied item. |
parentId | The parent's item id for the copied item. |
callback | The callback when the copy request has started. |
final String itemId = "0000000000000000000000";
final String copiedItemName = "My File Copy.txt"
final String newParentId = "0000000000000000000000";
final ItemReference parentReference = new ItemReference();
parentReference.id = newParentId;
final ICallback<AsyncMonitor<Item>> callback = new ICallback<AsyncMonitor<Item>>() {
@Override
public void success(final AsyncMonitor<Item> itemAsyncMonitor) {
Toast.makeText(getActivity(), "Started the copy session", Toast.LENGTH_LONG).show();
}
...
// Handle failure
};
oneDriveClient
.getDrive()
.getItems(itemId)
.getCopy(copiedItemName, parentReference)
.buildRequest()
.create(callback);
The result from the copy requests creation is a monitor that can be checked for status updates, return result of the operation, and automatically poll for the result.
Most applications will want to get the result to the user as soon as possible via a polling approach, the following shows how to get the resulting item from with a process notifications.
Name | Description |
---|---|
millisBetweenPoll | The time in milliseconds between progress updates. |
asyncMonitor | The name for the copied item. |
callback | The callback when the copy request has finished and progressed. |
final int millisBetweenPoll = 1000; // 1 second
final AsyncMonitor<Item> asyncMonitor = ...;
final IProgressCallback<Item> callback = new IProgressCallback<Item>() {
public void success(final Item item) {
Toast.makeText(getActivity(), "Item copied!", Toast.LENGTH_LONG).show();
}
public void progress(final int progress, final int progressMax) {
Toast.makeText(getActivity(), "Item copy process " + progress, Toast.LENGTH_SHORT).show();
}
...
// Handle failure
}
asyncMonitor
.pollForResult(millisBetweenPoll, callback);
Uploading a large file to OneDrive needs create upload session and uploading bytes to the session url.
You create a create session request for an item by constructing request builders getDrive
, getRoot
, getItemWithPath
, getCreateSession
, buildRequest
on the item, and then calling post
to start the create session request. The response is an upload session object which you call createUploadProvider
of the object to create an upload provider which handles large file uploading.
Name | Description |
---|---|
itemPath | The path to the file. |
chunkedUploadSessionDescriptor | The chunked upload session descriptor. |
oneDriveClient | The one drive client. |
fileStream | The input file stream. |
fileSize | The size of the file. |
uploadType | The upload class type. |
uploadOptions | The upload options. |
callback | The upload callback. |
chunkSize | The chunk size for each upload chunk, default is 5MiB. |
maxRetry | The max retry times for each upload chunk, default is 3. |
final String itemPath = "documents/file to copy.txt";
final Option uploadOptions = new QueryOption("@name.conflictBehavior", "fail");
final int chunkSize = 640 * 1024; //must be the multiple of 320KiB
final int maxRetry = 5;
final IProgressCallback<Item> callback = new IProgressCallback<Item>() {
@Override
public void progress(long current, long max) {
dialog.setProgress((int) current);
dialog.setMax((int) max);
}
@Override
public void success(Item item) {dialog.dismiss();
Toast.makeText(getActivity(),
application
.getString(R.string.upload_complete,
item.name),
Toast.LENGTH_LONG).show();
refresh();
}
@Override
public void failure(ClientException error) {
dialog.dismiss();
if (error.isError(OneDriveErrorCodes.NameAlreadyExists)) {
Toast.makeText(getActivity(),
R.string.upload_failed_name_conflict,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(),
application
.getString(R.string.upload_failed,
filename),
Toast.LENGTH_LONG).show();
}
}
}
oneDriveClient
.getDrive()
.getRoot()
.getItemWithPath(itemPath)
.getCreateSession(new ChunkedUploadSessionDescriptor())
.buildRequest()
.post()
.createUploadProvider(oneDriveClient, fileStream, fileSize, Item.class)
.upload(Collections.singletonList(uploadOptions),
callback,
chunkSize,
maxRetry);