---
source_url: https://www.pubnub.com/docs/sdks/android/api-reference/files
title: File Sharing API for Android SDK
updated_at: 2026-06-23T11:43:37.759Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# File Sharing API for Android SDK

:::warning Unsupported docs
PubNub no longer maintains Android SDK docs, but our [Java SDK](https://www.pubnub.com/docs/sdks/java) or [Kotlin SDK](https://www.pubnub.com/docs/sdks/kotlin) 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)

```java
pubnub.sendFile()
    .channel(String)
    .fileName(String)
    .inputStream(InputStream)
    .cipherKey(String)
    .ttl(Integer)
    .shouldStore(Boolean)
    .message(Object)
    .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](#publish-file-message).

### Sample code

```java
pubnub.sendFile()
    .channel("my_channel"
    .fileName("cat_picture.jpg")
    .inputStream(inputStream)
    .cipherKey("my_cipher_key")
    .message("Look at this photo!")
    .async(new PNCallback<PNFileUploadResult>() {
        @Override
        public void onResponse(PNFileUploadResult result, PNStatus status) {
            if (!status.isError()) {
                System.out.println("send timetoken: " + result.getTimetoken());
                System.out.println("send status: " + result.getStatus());
                System.out.println("send fileId: " + result.downloadFile().getId());
                System.out.println("send fileName: " + result.downloadFile().getName());
            }
            System.out.println("send status code: " + status.getStatusCode());
        }
    });
```

### Response

```java
{
  "timetoken": 15957709330808500,
  "status": 200,
  "file": {
      "id": "d9515cb7-48a7-41a4-9284-f4bf331bc770",
      "name": "cat_picture.jpg"
  }
}
```

### Returns

The `sendFile()` operation returns a `PNFileUploadResult` with the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `timetoken` | Long | A representation of the timetoken when the message was published. |
| `status` | Integer | Remote call return code. |
| `file` | PNBaseFile | Uploaded `file` information. |

`PNBaseFile` contains the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `id` | Long | `Id` of the uploaded file. |
| `name` | String | `Name` of the upload file. |

### Other examples

#### Send file with push notification

:::tip Trigger push notifications when sharing files
To send mobile push notifications when sharing a file, include `pn_apns` (for iOS) and/or `pn_fcm` (for Android) payloads in the `message` parameter of the `sendFile` method.
When PubNub detects these reserved keys, it automatically forwards the push notification to the appropriate push service (APNs or FCM) for all devices registered on the channel.
:::

```java
// Build push payloads using the helper
      PushPayloadHelper pushPayloadHelper = new PushPayloadHelper();

      // APNS (iOS) configuration
      PushPayloadHelper.APNSPayload apnsPayload = new PushPayloadHelper.APNSPayload();

      APS aps = new APS();
      Map<String, String> alert = new HashMap<>();
      alert.put("title", "New File");
      alert.put("body", "A file was shared");
      aps.setAlert(alert);
      aps.setSound("default");
      apnsPayload.setAps(aps);

      // APNS2 targeting
      Target target = new Target();
      target.setTopic("com.yourapp.bundleid");
      target.setEnvironment(PNPushEnvironment.PRODUCTION);
      // target.setExcludeDevices(Arrays.asList("senderDeviceToken")); // optional

      APNS2Configuration apns2Config = new APNS2Configuration();
      apns2Config.setTargets(Collections.singletonList(target));
      apns2Config.setVersion("v2");
      apns2Config.setAuthMethod(APNS2AuthMethod.TOKEN);

      apnsPayload.setApns2Configurations(Collections.singletonList(apns2Config));
      pushPayloadHelper.setApnsPayload(apnsPayload);

      // FCM (Android) configuration - use FCMPayloadV2 (FCMPayload is deprecated)
      FCMPayloadV2 fcmPayload = new FCMPayloadV2();

      FCMPayloadV2.Notification fcmNotification = new FCMPayloadV2.Notification();
      fcmNotification.setTitle("New File");
      fcmNotification.setBody("A file was shared");
      fcmPayload.setNotification(fcmNotification);

      // Optional: data payload
      Map<String, String> fcmData = new HashMap<>();
      fcmData.put("type", "file_shared");
      fcmData.put("channel", "my-channel");
      fcmPayload.setData(fcmData);

      pushPayloadHelper.setFcmPayloadV2(fcmPayload);

      // Common payload (your custom message data)
      Map<String, Object> commonPayload = new HashMap<>();
      commonPayload.put("text", "Check out this file!");
      pushPayloadHelper.setCommonPayload(commonPayload);

      // Build the final message
      Map<String, Object> message = pushPayloadHelper.build();

      InputStream fileInputStream = new FileInputStream("path/to/your/file.txt");

      // Send file with push notification
      pubnub.sendFile()
              .channel("my-channel")
              .fileName("example.txt")
              .inputStream(fileInputStream)
              .message(message)
              .async(result -> {
                  result.onSuccess(res -> {
                      System.out.println("File ID: " + res.getFile().getId());
                  }).onFailure(ex -> {
                      System.err.println("Error: " + ex.getMessage());
                  });
              });
```

For more details on push notification payload structure, see the [iOS Push](https://www.pubnub.com/docs/general/push/ios) and [Android Push](https://www.pubnub.com/docs/general/push/android) documentation. To prevent the sender from receiving their own push notification, add their device token to `excluded_devices` in the `pn_push` targets.

## List channel files

Retrieve a list of files uploaded to a `channel`.

### Method(s)

```java
pubnub.listFiles()
    .channel(String)
    .limit(Integer)
    .next(String)
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: StringDefault: n/a | Channel to get list of files. |
| `limit`Type: IntegerDefault: 100 | Number of files to return. Minimum value is 1, and maximum is 100. |
| `next`Type: StringDefault: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |

### Sample code

```java
pubnub.listFiles()
        .channel("my_channel")
        .async(new PNCallback<PNListFilesResult>() {
            @Override
            public void onResponse(PNListFilesResult result, PNStatus status) {
                if (!status.isError()) {
                    System.out.println("files status: " + result.getStatus());
                    System.out.println("files status: " + result.getNext());
                    System.out.println("files status: " + result.getCount());
                    System.out.println("files status: " + result.getCount());
                    for (PNUploadedFile file : result.getData()) {
                        System.out.println("files fileId: " + file.getId());
                        System.out.println("files fileName: " + file.getName());
                        System.out.println("files fileSize: " + file.getSize());
                        System.out.println("files fileCreated: " + file.getCreated());
                    }
                }
                System.out.println("files status code: " + status.getStatusCode());
            }
        });
```

### Response

```java
{
  "data":[
      {
      "name":"cat_picture.jpg",
      "id":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
      "size":25778,
      "created":"202007 - 26T13:42:06Z"
      }],
   "status": 200
   "totalCount": 1,
   "next": null,
   "prev": null
}
```

### Returns

The `listFiles()` operation returns a `PNListFilesResult` with the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `timetoken` | Long | A representation of the timetoken when the message was published. |
| `status` | Integer | Remote call return code. |
| `next` | String | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
| `count` | Integer | Number of files returned. |
| `data` | List | `List` of channel files. |

`PNUploadedFile` contains the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `id` | Long | ID of the uploaded file. |
| `name` | String | Name of the uploaded file. |
| `size` | Integer | `Size` of the uploaded file. |
| `created` | String | Time of creation. |

## Get file URL

Generate a URL that can be used to download a file from the target `channel`.

### Method(s)

```java
pubnub.getFileUrl()
    .channel(String)
    .fileName(String)
    .fileId(String)
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: String | Name of `channel` to which the file has been uploaded. |
| `fileName` *Type: String | Name under which the uploaded file is stored. |
| `fileId` *Type: String | Unique identifier for the file, assigned during upload. |

### Sample code

```java
pubnub.getFileUrl()
        .channel("my_channel")
        .fileName("cat_picture.jpg")
        .fileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
        .async(new PNCallback<PNFileUrlResult>() {
            @Override
            public void onResponse(PNFileUrlResult result, PNStatus status) {
                if (!status.isError()) {
                    System.out.println("getUrl fileUrl: " + result.getUrl());
                }
                System.out.println("getUrl status code: " + status.getStatusCode());
            }
        });
```

### Response

```java
{
  "url":"http://ps.pndsn.com/v1/files/demo/channels/my_channel/files/d9515cb7-48a7-41a4-9284-f4bf331bc770/cat_picture.jpg?pnsdk=PubNub-Java-Unified/4.32.0&timestamp=1595771548&uuid=pn-9ce9e988-8e04-40bf-90c4-ebe170478f7d"
}
```

### Returns

The `getFileUrl()` operation returns a `PNFileUrlResult` with the following property:

| Property Name | Type | Description |
| --- | --- | --- |
| `url` | String | `URL` to be used to download the requested file. |

## Download file

Download a file from the specified `channel`.

### Method(s)

```java
pubnub.downloadFile()
    .channel(String)
    .fileName(String)
    .fileId(String)
    .cipherKey(String)
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: String | Name of `channel` to which the file has been uploaded. |
| `fileName` *Type: String | Name under which the uploaded file is stored. |
| `fileId` *Type: String | Unique identifier for the file, assigned during upload. |
| `cipherKey`Type: String | Key used to decrypt downloaded data. If not provided, the SDK uses the `CipherKey` from the `PNConfiguration`. |

### Sample code

```java
pubnub.downloadFile()
        .channel("my_channel")
        .fileName("cat_picture.jpg")
        .fileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
        .cipherKey("my_cipher_key")
        .async(new PNCallback<PNDownloadFileResult>() {
            @Override
            public void onResponse(PNDownloadFileResult result, PNStatus status) {
                if (!status.isError()) {
                    System.out.println("downloadFile fileName: " + result.getFileName());
                    System.out.println("downloadFile byteStream: " + result.getByteStream());
                }
                System.out.println("downloadFile status code: " + status.getStatusCode());
            }
        });
```

### Response

```java
{
    "fileName": "cat_picture.jpg",
    "byteStream": <file data>
}
```

### Returns

The `downloadFile()` operation returns a `PNDownloadFileResult` with the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `fileName` | String | Name of the downloaded file. |
| `byteStream` | InputStream | Input stream containing all bytes of the downloaded file. |

## Delete file

Delete a file from the specified `channel`.

### Method(s)

```java
pubnub.deleteFile()
    .channel(String)
    .fileName(String)
    .fileId(String)
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: String | The `channel` from which to delete the file. |
| `fileName` *Type: String | Name of the file to be deleted. |
| `fileId` *Type: String | Unique identifier of the file to be deleted. |

### Sample code

```java
pubnub.deleteFile()
        .channel("my_channel")
        .fileName("cat_picture.jpg")
        .fileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
        .async(new PNCallback<PNDeleteFileResult>() {
            @Override
            public void onResponse(PNDeleteFileResult result, PNStatus status) {
                if (!status.isError()) {
                    System.out.println("delete status code: " + result.getStatus());
                }
                System.out.println("delete status code: " + status.getStatusCode());
            }
        });
```

### Response

```java
{
    "status": 200
}
```

### Returns

The `deleteFile()` operation returns a `PNDeleteFileResult` with the following property:

| Property Name | Type | Description |
| --- | --- | --- |
| `Status` | Integer | Returns a status code. |

## Publish file message

Publish file message from specified `Channel`.

### Method(s)

```java
pubnub.publishFileMessage()
    .channel(String)
    .fileName(String)
    .fileId(String)
    .message(Object)
    .meta(Object)
    .shouldStore(Boolean)
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: StringDefault: n/a | Name of `channel` to publish file message. |
| `fileName` *Type: StringDefault: n/a | Name of the file. |
| `fileId` *Type: StringDefault: n/a | Unique identifier of the file. |
| `message`Type: ObjectDefault: n/a | The payload. |
| `meta`Type: ObjectDefault: n/a | Meta data object which can be used with the filtering ability. |
| `shouldStore`Type: BooleanDefault: `true` | Whether to store this message in history. Set to `false` to not store it. By default, messages are stored according to the retention policy set on your key. |

### Sample code

```java
pubnub.publishFileMessage()
        .channel("my_channel")
        .fileName("cat_picture.jpg")
        .fileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
    .message("This is a sample message")
    .async(new PNCallback<PNFileUploadResult>() {
        @Override
        public void onResponse(PNFileUploadResult result, PNStatus status) {
            if (!status.isError()) {
                System.out.println("send timetoken: " + result.getTimetoken());
                System.out.println("send status: " + result.getStatus());
                System.out.println("send fileId: " + result.downloadFile().getId());
                System.out.println("send fileName: " + result.downloadFile().getName());
            }
            System.out.println("send status code: " + status.getStatusCode());
        }
    });
```

### Response

```java
{
  "timetoken": 15957709330808500,
  "status": 200,
  "file": {
      "id": "d9515cb7-48a7-41a4-9284-f4bf331bc770",
      "name": "cat_picture.jpg",
  }
}
```

### Returns

The `sendFile()` operation returns a `PNFileUploadResult` which contains the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `timetoken` | Long | The timetoken at which the message was published. |
| `status` | Integer | Remote call return code. |
| `file` | PNBaseFile | Uploaded `file` information. |

`PNBaseFile` contains the following properties:

| Property Name | Type | Description |
| --- | --- | --- |
| `id` | Long | Unique identifier of the uploaded file |
| `name` | String | `Name` of the uploaded file |