None of these methods needs a storage permission. Do not declare broad storage or “all files” access for them.
| Method | Returns / event | Pro |
|---|---|---|
readAppFile, writeAppFile, deleteAppFile, listAppFiles |
string, boolean, boolean, JSON string | no |
pickFile |
filePicked |
yes |
writeFile |
fileWritten |
yes |
copyToClipboard, pasteFromClipboard |
nothing, string | no |
shareText, shareFile, shareFileFromBase64 |
shareResult |
no |
printPage |
printResult |
no |
downloadDataUri, downloadBlobUrl |
downloadResult, blobDownloadError |
no |
Which storage to use
| Data | Good choice |
|---|---|
| Settings, small lists, scores | localStorage |
| Larger structured data, many records | IndexedDB |
| Text files you want to manage by name (JSON, CSV, notes) | readAppFile / writeAppFile |
| A file the user opens from their phone | pickFile or <input type="file"> |
| A file the user keeps outside the app | writeFile, a download, or sharing |
All app storage is deleted when the user uninstalls the app or clears its data. Offer an export if the data matters.
readAppFile
AndroidAPI.readAppFile(fileName) // → text, or "" if missing
Reads a UTF-8 text file from the app’s private storage. Returns an empty string if the file does not exist or cannot be read, so check for that before parsing.
const text = AndroidAPI.readAppFile('data/todos.json');
const todos = text ? JSON.parse(text) : [];
File names: relative paths only. Subfolders like data/todos.json are allowed; a leading /, .. segments and \ are rejected.
writeAppFile
AndroidAPI.writeAppFile(fileName, content) // → true / false
Writes text to private storage, creating subfolders as needed and replacing an existing file. Returns true on success. Write binary data as base64 text if you must store it here.
const ok = AndroidAPI.writeAppFile('data/todos.json', JSON.stringify(todos));
status.textContent = ok ? 'Saved' : 'Could not save';
deleteAppFile
AndroidAPI.deleteAppFile(fileName) // → true / false
Deletes a private file. Returns false if it did not exist or the name is invalid.
listAppFiles
AndroidAPI.listAppFiles() // → JSON string
Returns a JSON array of every private file, including those in subfolders: [{name, size, lastModified}]. name is the relative path, size is in bytes and lastModified is a Unix timestamp in milliseconds.
const files = JSON.parse(AndroidAPI.listAppFiles())
.filter(f => f.name.startsWith('notes/'))
.sort((a, b) => b.lastModified - a.lastModified);
pickFile
AndroidAPI.pickFile(mimeType, callbackId) // e.g. "text/*", "application/json", "*/*"
Pro. Opens Android’s file picker. The chosen file’s bytes arrive as raw base64 (not text, not a data URL). The limit is 16 MB.
Result event: filePicked {callbackId, success, content, error}. Errors: cancelled, file_too_large, pro_required (also as a general proRequired event).
window.addEventListener('filePicked', e => {
const r = e.detail;
if (r.callbackId !== 'import') return;
if (!r.success) return r.error === 'cancelled' || showMessage(r.error);
const bytes = Uint8Array.from(atob(r.content), c => c.charCodeAt(0));
const text = new TextDecoder('utf-8').decode(bytes); // keeps non-English characters correct
importBackup(JSON.parse(text));
});
AndroidAPI.pickFile('application/json', 'import');
A standard <input type="file"> also works without Pro and gives you a File object.
writeFile
AndroidAPI.writeFile(fileName, content, mimeType, callbackId)
Pro. Opens Android’s “save as” screen so the user chooses where to save (Downloads, Drive, etc.). content is written as UTF-8 text. For binary files such as images or PDFs, use a download or shareFileFromBase64 instead.
Result event: fileWritten {callbackId, success, error}. Errors include cancelled.
window.addEventListener('fileWritten', e => {
if (e.detail.callbackId === 'export') showMessage(e.detail.success ? 'Exported' : e.detail.error);
});
AndroidAPI.writeFile('backup.json', JSON.stringify(data), 'application/json', 'export');
copyToClipboard and pasteFromClipboard
AndroidAPI.copyToClipboard(text)
AndroidAPI.pasteFromClipboard() // → text, or ""
Copy plain text, or read the current clipboard text synchronously. There is no event. Android 13 and later show their own “Copied” message.
copyButton.onclick = () => { AndroidAPI.copyToClipboard(code.textContent); copyButton.textContent = 'Copied'; };
pasteButton.onclick = () => { input.value = AndroidAPI.pasteFromClipboard(); };
shareText
AndroidAPI.shareText(text)
Opens Android’s share sheet with text or a link. Result event: shareResult {kind: "text", success, error}. success means the sheet opened, not that the user actually shared.
AndroidAPI.shareText('My score: 1200 points in Block Runner!');
shareFile
AndroidAPI.shareFile(filePath, mimeType)
Shares an existing file. Pass either:
- a simple name (no
/) of a file written withwriteAppFile, such asreport.csv, or - the native
pathreturned by another method, such as a recording.
Result event: shareResult {kind: "file", success, error}. Errors: not_found, invalid_file_name. For files in subfolders or data you generate in JavaScript, use shareFileFromBase64.
AndroidAPI.writeAppFile('report.csv', csvText);
AndroidAPI.shareFile('report.csv', 'text/csv');
shareFileFromBase64
AndroidAPI.shareFileFromBase64(fileName, base64Data, mimeType)
Shares data you created in the page, such as a canvas image or a generated PDF. base64Data can be raw base64 or a data URL, up to 16 MB. Result event: shareResult. Errors: invalid_file_name, empty_data, file_too_large.
const png = canvas.toDataURL('image/png');
AndroidAPI.shareFileFromBase64('chart.png', png, 'image/png');
// Text with non-English characters: encode as UTF-8 first
const bytes = new TextEncoder().encode(csvText);
const b64 = btoa(String.fromCharCode(...bytes));
AndroidAPI.shareFileFromBase64('list.csv', b64, 'text/csv');
The standard navigator.share({title, text, files}) also works in generated apps and uses these methods.
printPage
AndroidAPI.printPage()
Opens Android’s print screen for the current page. The user can print or choose Save as PDF. Result event: printResult {success, status, error} where status is opened or failed. window.print() works too. Add a @media print stylesheet to hide buttons and navigation.
Downloads: links, Blob URLs and data URIs
Ordinary download links work in generated apps and save to the phone’s Downloads folder:
const blob = new Blob([csvText], { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'export.csv';
a.click();
You can also call the methods directly:
AndroidAPI.downloadDataUri(dataUri, fileName, mimeType)
AndroidAPI.downloadBlobUrl(blobUrl, fileName, mimeType)
Result event: downloadResult {fileName, success, error}. A Blob that cannot be read reports blobDownloadError {message}.
window.addEventListener('downloadResult', e => {
showMessage(e.detail.success ? `${e.detail.fileName} saved to Downloads` : e.detail.error);
});
AndroidAPI.downloadDataUri(canvas.toDataURL('image/png'), 'drawing.png', 'image/png');