Provides access to a document on Android devices. More...
#include <juce_AndroidDocument.h>
Public Member Functions | |
AndroidDocument () | |
Create a null document. | |
AndroidDocument (const AndroidDocument &) | |
AndroidDocument (AndroidDocument &&) noexcept | |
AndroidDocument & | operator= (const AndroidDocument &) |
AndroidDocument & | operator= (AndroidDocument &&) noexcept |
~AndroidDocument () | |
bool | operator== (const AndroidDocument &) const |
True if the URLs of the two documents match. | |
bool | operator!= (const AndroidDocument &) const |
False if the URLs of the two documents match. | |
bool | deleteDocument () const |
Attempts to delete this document, and returns true on success. | |
bool | renameTo (const String &newDisplayName) |
Renames the document, and returns true on success. | |
AndroidDocument | createChildDocumentWithTypeAndName (const String &type, const String &name) const |
Attempts to create a new nested document with a particular type and name. | |
AndroidDocument | createChildDirectory (const String &name) const |
Attempts to create a new nested directory with a particular name. | |
bool | hasValue () const |
True if this object actually refers to a document. | |
operator bool () const | |
Like hasValue(), but allows declaring AndroidDocument instances directly in 'if' statements. | |
std::unique_ptr< InputStream > | createInputStream () const |
Creates a stream for reading from this document. | |
std::unique_ptr< OutputStream > | createOutputStream () const |
Creates a stream for writing to this document. | |
URL | getUrl () const |
Returns the content URL describing this document. | |
AndroidDocumentInfo | getInfo () const |
Fetches information about this document. | |
AndroidDocument | copyDocumentToParentDocument (const AndroidDocument &target) const |
Experimental: Attempts to copy this document to a new parent, and returns an AndroidDocument representing the copy. | |
bool | moveDocumentFromParentToParent (const AndroidDocument ¤tParent, const AndroidDocument &newParent) |
Experimental: Attempts to move this document from one parent to another, and returns true on success. | |
NativeInfo | getNativeInfo () const |
Static Public Member Functions | |
static AndroidDocument | fromFile (const File &filePath) |
Create an AndroidDocument representing a file or directory at a particular path. | |
static AndroidDocument | fromDocument (const URL &documentUrl) |
Create an AndroidDocument representing a single document. | |
static AndroidDocument | fromTree (const URL &treeUrl) |
Create an AndroidDocument representing the root of a tree of files. | |
Provides access to a document on Android devices.
In this context, a 'document' may be a file or a directory.
The main purpose of this class is to provide access to files in shared storage on Android. On newer Android versions, such files cannot be accessed directly by a file path, and must instead be read and modified using a new URI-based DocumentsContract API.
Example use-cases:
Note that you probably do not need this class if your app only needs to access files in its own internal sandbox. juce::File instances should work as expected in that case.
AndroidDocument is a bit like the DocumentFile class from the androidx extension library, in that it represents a single document, and is implemented using DocumentsContract functions.
AndroidDocument::AndroidDocument | ( | ) |
Create a null document.
AndroidDocument::AndroidDocument | ( | const AndroidDocument & | ) |
|
noexcept |
AndroidDocument::~AndroidDocument | ( | ) |
|
static |
Create an AndroidDocument representing a file or directory at a particular path.
This is provided for use on older API versions (lower than 19), or on other platforms, so that the same AndroidDocument API can be used regardless of the runtime platform version.
If the runtime platform version is 19 or higher, and you wish to work with a URI obtained from a native file picker, use fromDocument() or fromTree() instead.
If this function fails, hasValue() will return false on the returned document.
|
static |
Create an AndroidDocument representing a single document.
The argument should be a URL representing a document. Such URLs are returned by the system file-picker when it is not in folder-selection mode. If you pass a tree URL, this function will fail.
This function may fail on Android devices with API level 18 or lower, and on non-Android platforms. If this function fails, hasValue() will return false on the returned document. If calling this function fails, you may want to retry creating an AndroidDocument with fromFile(), passing the result of URL::getLocalFile().
|
static |
Create an AndroidDocument representing the root of a tree of files.
The argument should be a URL representing a tree. Such URLs are returned by the system file-picker when it is in folder-selection mode. If you pass a URL referring to a document inside a tree, this will return a document referring to the root of the tree. If you pass a URL referring to a single file, this will fail.
When targeting platform version 30 or later, access to the filesystem via file paths is heavily restricted, and access to shared storage must use a new URI-based system instead. At time of writing, apps uploaded to the Play Store must target API 30 or higher. If you want read/write access to a shared folder, you must:
This function may fail on Android devices with API level 20 or lower, and on non-Android platforms. If this function fails, hasValue() will return false on the returned document.
AndroidDocument & AndroidDocument::operator= | ( | const AndroidDocument & | ) |
|
noexcept |
bool AndroidDocument::operator== | ( | const AndroidDocument & | ) | const |
True if the URLs of the two documents match.
bool AndroidDocument::operator!= | ( | const AndroidDocument & | ) | const |
False if the URLs of the two documents match.
bool AndroidDocument::deleteDocument | ( | ) | const |
Attempts to delete this document, and returns true on success.
bool AndroidDocument::renameTo | ( | const String & | newDisplayName | ) |
Renames the document, and returns true on success.
This may cause the document's URI and metadata to change, so ensure to invalidate any cached information about the document (URLs, AndroidDocumentInfo instances) after calling this function.
AndroidDocument AndroidDocument::createChildDocumentWithTypeAndName | ( | const String & | type, |
const String & | name ) const |
Attempts to create a new nested document with a particular type and name.
The type should be a standard MIME type string, e.g. "image/png", "text/plain".
The file name doesn't need to contain an extension, as this information is passed via the type argument. If this document is File-based rather than URL-based, then an appropriate file extension will be chosen based on the MIME type.
On failure, the returned AndroidDocument may be invalid, and will return false from hasValue().
AndroidDocument AndroidDocument::createChildDirectory | ( | const String & | name | ) | const |
Attempts to create a new nested directory with a particular name.
On failure, the returned AndroidDocument may be invalid, and will return false from hasValue().
bool AndroidDocument::hasValue | ( | ) | const |
True if this object actually refers to a document.
If this function returns false, you must not call any function on this instance other than the special member functions to copy, move, and/or destruct the instance.
Referenced by operator bool().
|
explicit |
Like hasValue(), but allows declaring AndroidDocument instances directly in 'if' statements.
References hasValue().
std::unique_ptr< InputStream > AndroidDocument::createInputStream | ( | ) | const |
Creates a stream for reading from this document.
std::unique_ptr< OutputStream > AndroidDocument::createOutputStream | ( | ) | const |
Creates a stream for writing to this document.
AndroidDocumentInfo AndroidDocument::getInfo | ( | ) | const |
Fetches information about this document.
AndroidDocument AndroidDocument::copyDocumentToParentDocument | ( | const AndroidDocument & | target | ) | const |
Experimental: Attempts to copy this document to a new parent, and returns an AndroidDocument representing the copy.
On failure, the returned AndroidDocument may be invalid, and will return false from hasValue().
This function may fail if the document doesn't allow copying, and when using URI-based documents on devices with API level 23 or lower. On failure, the returned AndroidDocument will return false from hasValue(). In testing, copying was not supported on the Android emulator for API 24, 30, or 31, so there's a good chance this function won't work on real devices.
bool AndroidDocument::moveDocumentFromParentToParent | ( | const AndroidDocument & | currentParent, |
const AndroidDocument & | newParent ) |
Experimental: Attempts to move this document from one parent to another, and returns true on success.
This may cause the document's URI and metadata to change, so ensure to invalidate any cached information about the document (URLs, AndroidDocumentInfo instances) after calling this function.
This function may fail if the document doesn't allow moving, and when using URI-based documents on devices with API level 23 or lower.
NativeInfo AndroidDocument::getNativeInfo | ( | ) | const |