You can use Agora RTM SDK to send and receive image or file messages.
The RTM SDK supports sending non-empty images and files that do not exceed 30 MB in size. Each image or file you upload to the Agora server stays for seven days and has a corresponding media ID. You can download the image or file from the Agora server with the media ID.
The following diagram shows how the file or image is sent and received:
You can use the RtmImageMessage
class to perform the following image operations:
You can use the RtmFileMessage
class to perform the following file operations:
The RTM SDK adds the RtmImageMessage
class and the RtmFileMessage
class to save and transfer media ID. Because the RtmImageMessage
class and the RtmFileMessage
class inherit from the RtmMessage
class, you can use existing peer-to-peer or channel messaging to transfer the RtmImageMessage
instance and the RtmFileMessage
instance. Thus, you can send and receive image or file messages.
The following steps show the general process of sending and receiving an image message:
Upload an image to the Agora server. When the image is successfully uploaded, the SDK returns an RtmImageMessage
instance by callback.
Sample code for uploading an image to the Agora server:
RtmRequestId requestId = new RtmRequestId();
mRtmClient.createImageMessageByUploading(fileName, requestId, new ResultCallback<RtmImageMessage>() {
@Override
public void onSuccess(RtmImageMessage rtmImageMessage) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
If you have a corresponding media ID for an image that is still on the Agora server, you can create an RtmImageMessage
instance with the following code:
RtmImageMessage rtmImageMessage = mRtmClient.createImageMessageByMediaId(mediaId);
(Optional) Set the width, height, or thumbnail of the image by the RtmImageMessage
instance.
Sample code for setting the width and height of the image:
rtmImageMessage.setWidth(width);
rtmImageMessage.setHeight(height);
Sample code for setting the thumbnail of the image:
rtmImageMessage.setThumbnail(byteArray);
Send the RtmImageMessage
instance via peer-to-peer or channel messaging to a user or channel. The RtmImageMessage
class inherits from the RtmMessage
class, enabling you to use peer-to-peer or channel messaging to send the RtmImageMessage
instance.
Sample code for sending a peer-to-peer image message:
mRtmClient.sendMessageToPeer(peerId, rtmImageMessage, new SendMessageOptions(), new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
Sample code for sending a channel image message:
mRtmChannel.sendMessage(rtmImageMessage, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
The user who receives the image message can receive the corresponding callback. You can get the media ID from the RtmImageMessage
instance and save the image by media ID.
Sample code for receiving a peer-to-peer image message:
@Override
public void onImageMessageReceivedFromPeer(RtmImageMessage rtmImageMessage, String peerId) {
}
Sample code for receiving a channel image message:
@Override
public void onImageMessageReceived(RtmImageMessage rtmImageMessage, RtmChannelMember rtmChannelMember) {
}
Typically, you can download an image to local storage via media ID.
RtmRequestId requestId = new RtmRequestId();
mRtmClient.downloadMediaToFile(
rtmImageMessage.getMediaId(),
filePath,
requestId,
new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
}
);
For scenarios that require quick save operations, you can download the image to memory:
RtmRequestId requestId = new RtmRequestId();
mRtmClient.downloadMediaToMemory(
rtmImageMessage.getMediaId(),
requestId,
new ResultCallback<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
}
);
If you need to cancel uploading or downloading an image, refer to the following example code:
mRtmClient.cancelMediaUpload(requestId, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
mRtmClient.cancelMediaDownload(requestId, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
The following steps show the general process of sending and receiving a file message:
Upload a file to the Agora server. When the file is successfully uploaded, the SDK returns an RtmFileMessage
instance by callback.
Sample code for uploading a file to the Agora server:
RtmRequestId requestId = new RtmRequestId();
mRtmClient.createFileMessageByUploading(filePath, requestId, new ResultCallback<RtmFileMessage>() {
@Override
public void onSuccess(RtmFileMessage rtmFileMessage) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
If you have a corresponding media ID for a file that is still in the Agora server, you can create an RtmFileMessage
instance with the following code:
RtmFileMessage rtmFileMessage = mRtmClient.createFileMessageByMediaId(mediaId);
(Optional) Set the thumbnail of the file by the RtmFileMessage
instance.
rtmFileMessage.setThumbnail(byteArray);
Send the RtmFileMessage
instance via peer-to-peer or channel messaging to a user or channel. The RtmFileMessage
class inherits from the RtmMessage
class, enabling you to use peer-to-peer or channel messaging to send the RtmFileMessage
instance.
Sample code for sending a peer-to-peer file message:
mRtmClient.sendMessageToPeer(peerId, rtmFileMessage, new SendMessageOptions(), new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
Sample code for sending a channel file message:
mRtmChannel.sendMessage(rtmFileMessage, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
The user who receives the file message can receive the corresponding callback. You can get the media ID from the RtmFileMessage
instance and save the file by media ID.
Sample code for receiving a peer-to-peer file message:
@Override
public void onFileMessageReceivedFromPeer(RtmFileMessage rtmFileMessage, String peerId) {
}
Sample code for receiving a channel file message:
@Override
public void onFileMessageReceived(RtmFileMessage rtmFileMessage, RtmChannelMember rtmChannelMember) {
}
Typically, you can download a file to local storage via media ID.
RtmRequestId requestId = new RtmRequestId();
mRtmClient.downloadMediaToFile(
rtmFileMessage.getMediaId(),
filePath,
requestId,
new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
}
);
For scenarios that require quick save operations, you can download the file to memory:
RtmRequestId requestId = new RtmRequestId();
mRtmClient.downloadMediaToMemory(
rtmFileMessage.getMediaId(),
requestId,
new ResultCallback<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
}
);
If you need to cancel uploading or downloading a file, refer to the following example code:
mRtmClient.cancelMediaUpload(requestId, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
mRtmClient.cancelMediaDownload(requestId, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
@Override
public void onFailure(ErrorInfo errorInfo) {
}
});
filePath
parameter for uploading and downloading methods can only accept strings with UTF-8 format. The path must be an absolute path.onSuccess
callback completes.RtmImageMessage
. The settings only exist as additional attributes and does not affect the uploaded image. The uploaded image is not resized or cropped.createFileMessageByMediaId
createImageMessageByMediaId
createFileMessageByUploading
createImageMessageByUploading
downloadMediaToMemory
downloadMediaToFile
cancelMediaUpload
cancelMediaDownload
onImageMessageReceived
onFileMessageReceived
onImageMessageReceivedFromPeer
onFileMessageReceivedFromPeer
onMediaUploadingProgress
onMediaDownloadingProgress