With Newzulu Platform SDK you can upload user content and retrieve it into your dashboard in seconds.

The first thing you want to be sure before starting is that the user is authenticated with Newzulu Platform. Be sure you followed the login guide and that your user is authenticated before starting.

1. Prepare your upload

To upload a media (photo or video) into Newzulu Platform, you have to provide the SDK with 2 things:

  • A File that points to your media
  • The UploadInfo containing info about your upload

Currently, the SDK supports the following types of file:

  • jpg, jpeg and png for images
  • mp4, mpeg and mpg for videos

With those data, you can create an upload object:

final Upload upload = new Upload(FILE_TO_UPLOAD, new UploadInfo.Builder()
    .setTitle("upload title") // This is optional
    .setDescription("upload description") // This is optional
    .build());

2. Start the upload

You are now ready to start your upload, here's how to do it:

getPlatformClient().startUpload(upload, new Upload.UploadResponseListener()
{
    @Override
    public void onResponse(@NonNull Upload upload)
    {
        // The upload completed with success
    }

    @Override
    public void onProgress(long uploadedBytes, long totalBytes)
    {
        // Called when the upload progress changed
    }

}, new Request.ErrorListener<PlatformUploadException>()
{
    @Override
    public void onError(@NonNull PlatformUploadException e)
    {
        // Called on error, see #3 to handle it
    }
});

3. Handle errors

The onError callback comes with a PlatformUploadException that contains 2 important things: the exception and state.

The exception

The exception contains informations about what gone wrong during the upload. You can call getCause().getType() on the exception to get the type of error, here's the list of possible values:

  • NETWORK: A network error occurred and the SDK unable to reach the remote service
  • NOT_LOGGED: You have called the upload method without authenticating the user before
  • INVALID_FILE: The file your provided is invalid. Either not available or with an invalid extension
  • SERVICE_ERROR: The remote service encountered an error, you should try again later
  • UNKNOWN: An unknown internal error occurred

If the error you got was INVALID_FILE there is no need to retry your upload since it will fail again.

Retry a failed upload

You can retry a failed upload using the UploadState given on error callback. Here's how to retrieve it:

final UploadState state = exception.getUploadState();
if( state != null )
{
    // State is available!
}

With the state, you can retry your upload using the resumeUpload method of the SDK:

getPlatformClient().resumeUpload(state, ...);

Note that this state is Parcelable so you can keep and pass it easily across your app.

Background upload

If you don't want to manage the upload progress and state by yourself, the SDK comes with a handy background upload that will handle your upload into a service.

It will generate a notification to inform the user of the upload state and will handle retry automatically.

It's a good idea to use this mode if you don't want to handle all the upload process: it will automatically use a background service and will ensure your upload is made even if the user leaves the app.

To start an upload in background mode, simply call the uploadBackground method:

uploadBackground(upload, context, thumbnailFile);

Notice the thumbnailFile parameter? It's an optional file you can pass to the method that points to a thumbnail of the media you are uploading and that will be use in the notification shown to the user. It's fully optional, and you have to made sure avoid passing a too large image. If you don't want to use it, just pass null.

Note that using this mode you won't have any callback into your app about the upload state.