Loading...
Searching...
No Matches
AndroidDocument Class Reference

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
 
AndroidDocumentoperator= (const AndroidDocument &)
 
AndroidDocumentoperator= (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< InputStreamcreateInputStream () const
 Creates a stream for reading from this document.
 
std::unique_ptr< OutputStreamcreateOutputStream () 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 &currentParent, 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.
 

Detailed Description

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:

  • After showing the system open dialog to allow the user to open a file, pass the FileChooser's URL result to AndroidDocument::fromDocument. Then, you can use getInfo() to retrieve information about the file, and createInputStream to read from the file. Other functions allow moving, copying, and deleting the file.
  • Similarly to the 'open' use-case, you may use createOutputStream to write to a file, normally located using the system save dialog.
  • To allow reading or writing to a tree of files in shared storage, you can show the system open dialog in 'selects directories' mode, and pass the resulting URL to AndroidDocument::fromTree. Then, you can iterate the files in the directory, query them, and create new files. This is a good way to store multiple files that the user can access from other apps, and that will be persistent after uninstalling and reinstalling your app.

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.

Constructor & Destructor Documentation

◆ AndroidDocument() [1/3]

AndroidDocument::AndroidDocument ( )

Create a null document.

◆ AndroidDocument() [2/3]

AndroidDocument::AndroidDocument ( const AndroidDocument & )

◆ AndroidDocument() [3/3]

AndroidDocument::AndroidDocument ( AndroidDocument && )
noexcept

◆ ~AndroidDocument()

AndroidDocument::~AndroidDocument ( )

Member Function Documentation

◆ fromFile()

static AndroidDocument AndroidDocument::fromFile ( const File & filePath)
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.

◆ fromDocument()

static AndroidDocument AndroidDocument::fromDocument ( const URL & documentUrl)
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().

◆ fromTree()

static AndroidDocument AndroidDocument::fromTree ( const URL & treeUrl)
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:

  • Use a native FileChooser in canSelectDirectories mode, to allow the user to select a folder that your app can access. Your app will only have access to the contents of this directory; it cannot escape to the filesystem root. The system will not allow the user to grant access to certain locations, including filesystem roots and the Download folder.
  • Pass the URI that the user selected to fromTree(), and use the resulting AndroidDocument to read/write to the file system.

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.

◆ operator=() [1/2]

AndroidDocument & AndroidDocument::operator= ( const AndroidDocument & )

◆ operator=() [2/2]

AndroidDocument & AndroidDocument::operator= ( AndroidDocument && )
noexcept

◆ operator==()

bool AndroidDocument::operator== ( const AndroidDocument & ) const

True if the URLs of the two documents match.

◆ operator!=()

bool AndroidDocument::operator!= ( const AndroidDocument & ) const

False if the URLs of the two documents match.

◆ deleteDocument()

bool AndroidDocument::deleteDocument ( ) const

Attempts to delete this document, and returns true on success.

◆ renameTo()

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.

◆ createChildDocumentWithTypeAndName()

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().

◆ createChildDirectory()

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().

◆ 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().

◆ operator bool()

AndroidDocument::operator bool ( ) const
explicit

Like hasValue(), but allows declaring AndroidDocument instances directly in 'if' statements.

References hasValue().

◆ createInputStream()

std::unique_ptr< InputStream > AndroidDocument::createInputStream ( ) const

Creates a stream for reading from this document.

◆ createOutputStream()

std::unique_ptr< OutputStream > AndroidDocument::createOutputStream ( ) const

Creates a stream for writing to this document.

◆ getUrl()

URL AndroidDocument::getUrl ( ) const

Returns the content URL describing this document.

◆ getInfo()

AndroidDocumentInfo AndroidDocument::getInfo ( ) const

Fetches information about this document.

◆ copyDocumentToParentDocument()

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.

See also
AndroidDocumentInfo::canCopy

◆ moveDocumentFromParentToParent()

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.

◆ getNativeInfo()

NativeInfo AndroidDocument::getNativeInfo ( ) const

The documentation for this class was generated from the following file:
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram