Skip to main content

File Operations

This guide describes the methods to manage files.

Copy#

The 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.

Move#

The 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.

Rename#

The 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 characters
  • STARTS_WITH_SPACE - file name must not start with a space
  • ENDS_WITH_SPACE - file name must not end with a space
  • ENDS_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 code
  • uri: Uri? - null
  • path: String? - null
  • fileName: String? - destination name of erroneous file

Links - FileItem, InvalidFileNameException, InvalidFileNameException.ErrorCode.

Create Directory#

The 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.

Links - FileItem, Rename

Deleting#

The library provides delete and multi-delete functions.

Delete#

The 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 Delete#

The 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 Link#

The library provides the public link functionality for the stored files and folders.

Create Public Link#

The 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 Link#

The 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 Link#

The 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.