File Sharing API for Android SDK
Unsupported docs
PubNub no longer maintains Android SDK docs, but our Java SDK or Kotlin SDK are fully compatible with the Android platform and you can use them to build mobile apps, ensuring stable software development.
Use the Files API to upload and share files up to 5 MB on PubNub. Common use cases include social apps that share images and healthcare apps that share medical records.
When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.
Send file
Upload a file or data to the specified channel.
Method(s)
1pubnub.sendFile()
2 .channel(String)
3 .fileName(String)
4 .inputStream(InputStream)
5 .cipherKey(String)
6 .ttl(Integer)
7 .shouldStore(Boolean)
8 .message(Object)
9 .meta(Object)
| Parameter | Type | Required | Default | Description |
| :-------- | :-------- | :-------- |: --- | :--- |
| channel | String | Yes | n/a | Channel for the file. |
| fileName | String | Yes | n/a | Name of the file to send. |
| inputStream | InputStream | Yes | n/a | Input stream with file content. |
| cipherKey | String | Optional | PNConfiguration#cipherKey | Key used to encrypt uploaded data. If not provided, the cipherKey in PNConfiguration is used if configured. |
| ttl | Integer | Optional | n/a | How long the message should be stored in the channel's storage. |
| shouldStore | Boolean | Optional | true | Whether to store the published file message in the channel history. |
| message | Object | Optional | n/a | Message to send along with the file to the specified channel. |
| meta | Object | Optional | n/a | Metadata object that can be used with the filtering capability. |
For details on the method that publishes the file message, see Publish file message.
Sample code
1pubnub.sendFile()
2 .channel("my_channel"
3 .fileName("cat_picture.jpg")
4 .inputStream(inputStream)
5 .cipherKey("my_cipher_key")
6 .message("Look at this photo!")
7 .async(new PNCallback<PNFileUploadResult>() {
8 @Override
9 public void onResponse(PNFileUploadResult result, PNStatus status) {
10 if (!status.isError()) {
11 System.out.println("send timetoken: " + result.getTimetoken());
12 System.out.println("send status: " + result.getStatus());
13 System.out.println("send fileId: " + result.downloadFile().getId());
14 System.out.println("send fileName: " + result.downloadFile().getName());
15 }
show all 18 lines