microsoftgraph / msgraph-sdk-android

Microsoft Graph SDK for Android! https://graph.microsoft.io
Other
51 stars 43 forks source link

Upload a File #15

Closed diogowbrito closed 8 years ago

diogowbrito commented 8 years ago

Is there a way to create a file and upload contents, such as referenced here?

After having looked at the SDK code I didn't find a way to do so.

peternied commented 8 years ago

Thanks for the question @diogowbrito, OneDrive upload and download scenarios are supported, you can see a end-to-end example with this SDK with the OneDrive Explorer.

Upload a file, full example here:

final String parentItemId = // The id for the parent folder this item is being uploaded into
final String filename = // The name of the file being uploaded
final byte[] fileInMemory = // The contents of the file to upload

graphServiceClient
    .getMe()
    .getDrive()
    .getItems(parentItemId)
    .getChildren()
    .byId(filename)
    .getContent()
    .buildRequest()
    .put(fileInMemory,
        new IProgressCallback<DriveItem>() {
            @Override
            public void success(final DriveItem item) {
                // Handle success
            }

            @Override
            public void failure(final ClientException error) {
                // Handle failure
            }

            @Override
            public void progress(final long current, final long max) {
                // Handle progress updates
            }
        });

Download a file, thumbnail example here,

final String itemId = //The id of the item to download
new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        final InputStream fileContents = graphServiceClient
                .getDrive()
                .getItems(itemId)
                .getContent()
                .buildRequest()
                .get();

        return null;
    }
};
diogowbrito commented 8 years ago

Thanks @peternied. Exactly what I was looking for.