Browse SDKs · Android
SDKsAndroid

Upload a file

Upload a standalone file with the Android SDK and retrieve its remote resource URL.

Copy

uploadFile() uploads a standalone file for uses such as a user avatar, group avatar, profile attachment, or other application resource. The upload does not belong to a message and does not automatically create a message or update a profile.

Parameters

ParameterTypeRequiredDescription
baseOnBase<String>YesReceives the final success or failure result for the upload request.
listenerOnPutFileListenerYesReceives file, part, hash, and completion information for this task.
putArgs.filepathStringYesA local file path readable by the Android application.
putArgs.nameStringYesThe upload file name. PutArgs initially uses the supplied path; replace it with the original file name when appropriate.
putArgs.cuaseStringNoAn application-defined upload reason. The spelling follows the Android SDK PutArgs declaration.
File file = new File(localPath);
PutArgs args = new PutArgs(file.getAbsolutePath());
args.name = file.getName();
args.cuase = "user-avatar";

OpenIMClient.getInstance().uploadFile(
    new OnBase<String>() {
        @Override
        public void onSuccess(String result) {
            // result is the final upload request result.
        }

        @Override
        public void onError(int code, String error) {
            // code and error describe the failure.
        }
    },
    new OnPutFileListener() {
        @Override
        public void open(long size) {
            // size is the size of the file to upload.
        }

        @Override
        public void uploadID(String id) {
            // id is the upload identifier generated by the SDK.
        }

        @Override
        public void uploadComplete(long fileSize, long streamSize, long storageSize) {
            // File, stream, and storage sizes are now available.
        }

        @Override
        public void complete(long size, String url, long type) {
            // url is the remote URL of the uploaded resource.
        }
    },
    args
);

Callbacks

OnPutFileListener provides the following callbacks and parameters:

CallbackParametersDescription
opensizeOpens a file of the given size; use it to initialize the task.
partSizepartSize, numThe size of each part and the total number of parts.
hashPartProgressindex, size, partHashThe part index, size, and hash after a part is hashed.
hashPartCompletepartsHash, fileHashThe combined part hash and final file hash after all parts are hashed.
uploadIDuploadIDThe upload identifier generated by the SDK.
uploadPartCompleteindex, partSize, partHashThe part index, size, and hash when a part upload completes.
uploadCompletefileSize, streamSize, storageSizeFile size, stream size, and storage size for the completed file-upload stage.
completesize, url, typeThe final file size, remote URL, and resource type after the upload completes.

The URL returned by complete() can be used later to update an avatar, store an attachment address, or create a URL-based media message. Upload success only means that the resource was written remotely; profile updates, message creation, and message sending remain separate API calls.