App Inventor Extensions


File Extension

See the App Inventor Extensions document about how to use an App Inventor Extension.

For questions about this extension, bug reports and feature requests, please start a new thread in the App Inventor Extensions forum. Thank you.

Sept 6th, 2015: Fixed the delete button to delete the extension and added a help text for the extension.

Sept 7th, 2015: Added the version number (version 1 as of 2015-09-07) in the help text of the extension.

Oct 12th, 2015: Rebuilt the extension for App Inventor version ai2extensions4 and Companion version 233.cdk2.

Nov 16th, 2015: Copy and Delete blocks added, built for App Inventor version ai2extensions6 and Companion version 235.cdk2.

Mar 11th, 2016: FileList: includeSubdirectories option added, FileListAsync method added.

Mar 25th, 2016: Version 3a: better error message will be provided for Copy block, see also here

Mar 30th, 2016: Version 3b: bug fix for Exists method, see also here

May 5th, 2016: Version 4: GetFileName from media content uri method added

May 8th, 2016: Version 5: DirectoryList method added

Aug 11th, 2016: Version 5b: avoid DX execution failed error: build each extension separately

Dec 19th, 2016: Version 6+7: GetUri, AvailableStorageDirectories, AvailableSize methods added

Dec 30th, 2016: Version 7a: AvailableSize method now works for API Level 9 and above

Description

This extension is related to the file component.
Required permissions: android.permission.READ_EXTERNAL_STORAGE, android.permission.WRITE_EXTERNAL_STORAGE

Properties


Returns whether Warnings should be suppressed


Specifies whether Warnings should be suppressed

Methods


Copy file.
Prefix the filename with / (i.e. relative path) to copy a file on the SD card. For instance /myFile.txt will copy the file /mnt/sdcard/myFile.txt. To copy assets packaged with an application (also works for the Companion) start the fromFilename with // (two slashes). However you can't copy files to the assets. If a fileName starts with file:/// you can specify a complete path to the file.

Note: For copying a file to an external sdcard, there are the following restrictions: an app can only write to its own directory but can't create its own directory (example: appinventor.ai_MxyzptlkHavok.fileTest). Subdirectories will only be created if an app is writing to its own directory. Thank you MxyzptlkHavok for your tests!
If you try to write to another directory on the external sdcard, you will get an error message "open failed: EACCES (Permission denied)", see also here.


Delete file.
Prefix the filename with / (i.e. relative path) to delete a file on the SD card. For instance /myFile.txt will delete the file /mnt/sdcard/myFile.txt. You can't delete files stored in the assets! If a fileName starts with file:/// you can specify a complete path to the file.

Note: To delete a file from an external sdcard is currently an experimental feature, see also this thread.


Return a list of subdirectories of a given directory.
The complete path to these subdirectories is provided. If a directory starts with file:/// you can specify a complete path to the directory. The result list does NOT include subdirectories of subdirectories.


Checks whether a file or directory exists.
Prefix the filename with / (i.e. relative path) to look for the file on the SD card. For instance /myFile.txt will look for the file /mnt/sdcard/myFile.txt. To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a fileOrDirectoryName starts with file:/// you can specify a complete path to the file of directory."


Return a list of filenames of a given directory.
Use this method for specific directories to get back a result immediately. To get a list for a complete device or sdcard, which may take a few seconds, use the FileListAsync method instead.
The complete path to these files is provided. You can filter by extension. To get a list of all files, use * as extension. If a directory starts with file:/// you can specify a complete path to the directory. To include files of all subdirectories, set includeSubdirectories to true, else to false.
Thank you Shorty for being the sponsor of the includeSubdirectories option.


Create a list of filenames of a given directory.
The result will be provided in the AfterFileListAsync event.
The complete path to these files is provided. You can filter by extension. To get a list of all files, use * as extension. If a directory starts with file:/// you can specify a complete path to the directory. To include files of all subdirectories, set includeSubdirectories to true, else to false.
Thank you Shorty for being the sponsor of the includeSubdirectories option.


Checks whether it is a file or directory.
Prefix the filename with / (i.e. relative path) to look for the file on the SD card. For instance /myFile.txt will look for the file /mnt/sdcard/myFile.txt. If fileOrDirectoryName starts with file:/// you can specify a complete path to the file or directory.


Return file length of a file.
Prefix the filename with / (i.e. relative path) to look for the file on the SD card. For instance /myFile.txt will look for the file /mnt/sdcard/myFile.txt. To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a fileName starts with file:/// you can specify a complete path to the file.


Get path and filename from media content uri
Thank you Matthew for being the sponsor of this block.


Get content uri from filename


Return a list of available storage directories.
The method uses this snippet. Thank you HendraWD and Dmitriy
Thank you Hussein for being the sponsor of this method!


Return available size of given storage directory in Bytes.
To get the value in KB, divide by 1024. To get the value in MB, divide by another 1024. To get the value in GB, divide by another 1024.

Minimum API Level for this method is API Level 9 (Android 2.3)
Thank you Hussein for being the sponsor of this method and thank you SteveJG for your additional tests!

Events


Event indicating that creating a list of files of a given directory has finished.

Example Use


Example App: Test Copy and Delete blocks


Example App: Test FileList blocks


Example App: Test Available Storage Size blocks


Example App: Jukebox

Get a list of all music files of the Music directory of your device and play your music in random order.
Jukebox image by Pixabay. Thank you!

Questions and Answers

Q1: Very helpful component! How to get a file list on External SDCard? I can do only on phone memory?
A: use the full path to the directory, example: file:///storage/extSdCard/myDirectory or file:///storage/sdcard1/myDirectory or file:///storage/ext_sd/myDirectory. The full path to the external sdcard is device dependent and might be different for your device...

Q2: Please advise me how to delete a file location that retrieve from ActivityStarter.ResultUri. For ex. the result is content://media/external/image/media/7004 How can we know the real file path?
A: This is a "content uri". Thank you Matthew for being the sponsor of the GetFileName method.

Q3: Concerning the FileList method: Is there a way to exclude sub directories? I would like to target JUST the root.
A: This is currently not possible, the FileList method returns a list of filenames of a given directory including all subdirectories. However I could add that as option after having received a donation of 10 USD. Thank you Shorty for being the sponsor of that option.

Q4: Is there a way to delete a folder? Do you plan to implement this in your File Extension component, perhaps with a sponsorship?
A: I did not implement this to prevent people deleting a complete sdcard unintentionally. See this thread how to delete a folder.

Do you need help with your App Inventor project?

I can help you: for one hour I charge 25 USD.
Please contact me at info [at] puravidaapps [dot] com for details!
For questions about App Inventor, please use the App Inventor forum. Thank you.

Terms and Conditions

Download


If you find this example helpful and you are using it in one of your projects,
be nice and donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download TaifunFile extension (aix file)

Download Test Copy and Delete (aia file)
Download Test FileList (aia file)
Download Available Storage Size Test (aia file)
Download Jukebox (aia file)

Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | News | Forums | Links | Search | Contact