Google is committed to advancing racial equity for Black communities. See how.

Payload.File

public static class Payload.File extends Object

Represents a file in local storage on the device.

Public Method Summary

File
asJavaFile()
This method is deprecated. use asUri() instead.
ParcelFileDescriptor
asParcelFileDescriptor()
Gets the ParcelFileDescriptor from which to read the data of this File; useful when reading the File in a streaming fashion, before the entire contents have arrived from the remote endpoint.
Uri
asUri()
Gets a file Uri for which the client package has read and write permissions (see openInputStream(Uri) or openFileDescriptor(Uri, String)).
void
close()
Closes the ParcelFileDescriptor to release resource.
long
getSize()
Gets the size of this Payload.File in bytes.

Inherited Method Summary

Public Methods

public File asJavaFile ()

This method is deprecated.
use asUri() instead.

Gets the File that backs this File. When receiving a File, this File will be written to the device's Downloads folder (as returned by getExternalStoragePublicDirectory(String)), and can then be copied or moved (read on for how to do this safely) to your app's private storage, if necessary.

Note that when receiving a Payload.File, the call to onPayloadReceived(String, Payload) occurs at the start of the data transfer; i.e., at the time of the call, this File exists but may not yet contain all of its data. Wait for updates to onPayloadTransferUpdate(String, PayloadTransferUpdate) to indicate the transfer has completed before attempting to move the File. If you want to read the data as it comes in, use asParcelFileDescriptor() to get a streaming interface to the incoming Payload.File data.

Note: The file is saved in the local device's Downloads folder under a generic name with no extension. The client app is responsible for renaming this File and adding an appropriate extension, if necessary, and all this (and possibly additional) metadata should be transmitted by the remote device out-of-band (likely using a Payload of type BYTES).

public ParcelFileDescriptor asParcelFileDescriptor ()

Gets the ParcelFileDescriptor from which to read the data of this File; useful when reading the File in a streaming fashion, before the entire contents have arrived from the remote endpoint.

public Uri asUri ()

Gets a file Uri for which the client package has read and write permissions (see openInputStream(Uri) or openFileDescriptor(Uri, String)). Read and write permissions can be persisted across reboots using takePersistableUriPermission(Uri, int). Alternatively, you can copy the file contents to a location under your control and delete the original file. The uri will be non-null when receiving file payloads. When sending file payloads, the uri may be null, i.e. when the payload is created by fromFile(ParcelFileDescriptor)).

public void close ()

Closes the ParcelFileDescriptor to release resource.

public long getSize ()

Gets the size of this Payload.File in bytes. For an incoming File, this is the expected size of the File once all data has been received. For an outgoing File, this is simply the size of the File or the available data in the ParcelFileDescriptor at the time of construction.