SDKsAndroid
Upload a file
Upload a standalone file with the Android SDK and retrieve its remote resource URL.
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
| Parameter | Type | Required | Description |
|---|---|---|---|
base | OnBase<String> | Yes | Receives the final success or failure result for the upload request. |
listener | OnPutFileListener | Yes | Receives file, part, hash, and completion information for this task. |
putArgs.filepath | String | Yes | A local file path readable by the Android application. |
putArgs.name | String | Yes | The upload file name. PutArgs initially uses the supplied path; replace it with the original file name when appropriate. |
putArgs.cuase | String | No | An 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:
| Callback | Parameters | Description |
|---|---|---|
open | size | Opens a file of the given size; use it to initialize the task. |
partSize | partSize, num | The size of each part and the total number of parts. |
hashPartProgress | index, size, partHash | The part index, size, and hash after a part is hashed. |
hashPartComplete | partsHash, fileHash | The combined part hash and final file hash after all parts are hashed. |
uploadID | uploadID | The upload identifier generated by the SDK. |
uploadPartComplete | index, partSize, partHash | The part index, size, and hash when a part upload completes. |
uploadComplete | fileSize, streamSize, storageSize | File size, stream size, and storage size for the completed file-upload stage. |
complete | size, url, type | The 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.
Was this page helpful?