File Operations
This guide describes the methods to manage files.
#
CopyThe following code example demonstrates copying a file.
var disposable: Disposable? = null
// File item to copyval fileItem: FileItem
// Destination full path without ending file name.val destination: String = "/destination/full/path"
// If true - overwrite existing file (if any).val overwrite: Boolean = true
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.copy(fileItem, destination, overwrite) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
MoveThe following code example demonstrates moving a file.
var disposable: Disposable? = null
// File item to moveval fileItem: FileItem
// Destination full path without ending file name.val destination: String = "/destination/full/path"
// If true - overwrite existing file (if any).val overwrite: Boolean = true
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.move(fileItem, destination, overwrite) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
RenameThe following code example demonstrates renaming a file.
var disposable: Disposable? = null
// File item to moveval fileItem: FileItem
// New name must contain just name, not path.val newName: String = "CucumberRick"
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.rename(fileItem, newName) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
When renaming a file, there is a set of restrictions on their names. An attempt of specifying invalid file name throws the InvalidFileNameException
exception. For each constraint, there is the corresponding InvalidFileNameException.ErrorCode
value:
INVALID_LENGTH
- file name must be no more than 255 charactersSTARTS_WITH_SPACE
- file name must not start with a spaceENDS_WITH_SPACE
- file name must not end with a spaceENDS_WITH_DOT
- file name must not end with a period
Given the error code, an instance of the InvalidFileNameException
class contains the following information:
code: ErrorCode
- error codeuri: Uri?
- nullpath: String?
- nullfileName: String?
- destination name of erroneous file
Links - FileItem, InvalidFileNameException, InvalidFileNameException.ErrorCode.
#
Create DirectoryThe following code example demonstrates creating a directory.
var disposable: Disposable? = null
// Full path of new directory.val path: String = "/back/to/the/future"
// Timestamp of new directoryval modifiedAt: Long = 1445412480
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.createDirectory(path, modifiedAt) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
When creating a new directory, there is a set of restrictions on its name. An attempt of specifying invalid directory name throws the InvalidFileNameException
exception. Constraints and error codes are the same as for Rename operation. For details see Rename.
#
DeletingThe library provides delete and multi-delete functions.
#
DeleteThe following code example demonstrates deleting a file.
var disposable: Disposable? = null
// Dile item to be deleted.val fileItem: FileItem
// Delete directory only if it's empty.val onlyEmpty: Boolean = false
// Ff true, file deleted permanently.val overwrite: Boolean = false
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.delete(fileItem, onlyEmpty, withoutTrash) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
Multi DeleteThe following code example demonstrates deleting multiple files.
var disposable: Disposable? = null
// List of file items to delete.val fileItems: List<FileItem>
// If true - non-empty directories will not be deleted.val onlyEmpty: Boolean = false
// If true - file items will be removed permanently.val withoutTrash: Boolean = false
// ...
// Returns Completable that calls onComplete when operation is done.// You have to subscribe on returned Completable in order to receive updates.disposable = FileManager.operations.multiDelete(fileItems, onlyEmpty, withoutTrash) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onComplete = { // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
Public LinkThe library provides the public link functionality for the stored files and folders.
#
Create Public LinkThe following code example demonstrates creating a public link.
var disposable: Disposable? = null
// File item to create link.val fileItem: FileItem
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.createLink(fileItem) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it: Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
Get Stream LinkThe following code example shows how to get a link to a file.
var disposable: Disposable? = null
// File item to getting link.val fileItem: FileItem
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.getStreamLink(fileItem) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ // it: String! // do something }, { // it: Throwable // do something })
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.
#
Delete Public LinkThe following code example demonstrates deleting a public link.
var disposable: Disposable? = null
// File item to unshare.val fileItem: FileItem
// ...
// Returns single that calls success when operation is completed.// You have to subscribe on returned observable in order to receive updates.disposable = FileManager.operations.deleteLink(fileItem) .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( onSuccess = { // it: FileItem! // do something }, onError = { // it - Throwable // do something } )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()
Links - FileItem.