Dynamic Resources
Since API level 6.0.3
The Application.DynamicResource module allows an app to build resources from binary data at runtime.
Unlike compiled resources, dynamic resources are not bundled in the executable and do not have identifiers in Rez. The app supplies a Lang.ByteArray containing the resource data to DynamicResource.build(). This call builds the resource; it does not download the data.
Support depends on the device and the resource being built. Check for Application has :DynamicResource before calling the module on devices that may not support it. The APIs used to display, play, or access the result have their own device support and permission requirements.
Building Resources
To build a resource, call DynamicResource.build().
For images, animations, and audio, the call returns the built resource, or null if building fails. The returned type depends on :type, so check the result before using it. Persisted content is handled asynchronously and returns its result through :callback instead.
The data must be in a format supported by the device. The formats accepted at runtime are not necessarily the same as those supported by the resource compiler. See the API documentation for image formats, animation formats, and audio container formats.
Images
Building an image returns a WatchUi.BitmapResource. If :format is omitted or null, the system attempts to detect the format from the data. When a GIF is built as an image, only its first frame is loaded.
The following function builds an image from a complete encoded byte array. It returns null if the module is unavailable or no bitmap is returned:
import Toybox.Application;
import Toybox.Application.DynamicResource;
import Toybox.Lang;
import Toybox.WatchUi;
function buildImage(data as ByteArray) as BitmapResource? {
if (Application has :DynamicResource) {
var resource = DynamicResource.build({
:type => DynamicResource.TYPE_IMAGE,
:data => data
});
if (resource instanceof BitmapResource) {
return resource;
}
}
return null;
}Audio
Building audio returns an Output.DynamicAudioSource that can be passed to Output.play(). Supply the complete encoded audio data and a :format value from Audio.ContainerFormat. This option describes the container, not the full Audio.AudioFormat dictionary used by audio builders and streaming sources.
Playback requires the AudioOutput permission and is available only in foreground apps, not watch faces, data fields, or widgets. See Manifest and Permissions for permission declarations and Audio for playback controls and device support.
The following function builds and plays a complete audio source. It returns true when a source is passed to the output module, or false if no audio source is returned. Playback errors are reported separately by the output module:
import Toybox.Application.DynamicResource;
import Toybox.Audio;
import Toybox.Audio.Output;
import Toybox.Lang;
function playDynamicAudio(data as ByteArray,
format as Audio.ContainerFormat) as Boolean {
var source = DynamicResource.build({
:type => DynamicResource.TYPE_AUDIO,
:format => format,
:data => data
});
if (source instanceof Output.DynamicAudioSource) {
Output.play(source, 0);
return true;
}
return false;
}If the data arrives in multiple buffers, use an Output.DynamicAudioSourceBuilder to assemble it before playback. To supply data while playback is in progress, use an Output.StreamingAudioSource. These approaches are described in Audio Sources.
Persisted Content
Dynamic resources can also import content, such as a FIT or GPX file, into the device's saved content. This requires the PersistedContent permission and device support for the content being imported. Unlike an image or audio source, the imported content is stored on the device for later access through Toybox.PersistedContent. See Downloading Content for more information.
Supply a callback matching DynamicResource.PersistedContentCallback. The build call returns null; wait for the callback before accessing the imported content.
The following example imports a complete file and prints the names of the imported items. It checks the status and iterator before accessing the result:
import Toybox.Application.DynamicResource;
import Toybox.Lang;
import Toybox.PersistedContent;
import Toybox.System;
class ContentImporter {
function initialize() {
}
function importContent(data as ByteArray) as Void {
DynamicResource.build({
:type => DynamicResource.TYPE_PERSISTED_CONTENT,
:data => data,
:callback => method(:onContentImported)
});
}
function onContentImported(result as {
:status as DynamicResource.Status,
:iterator as PersistedContent.Iterator?,
:count as Number?
}) as Void {
var iterator = result[:iterator];
if (result[:status] != DynamicResource.STATUS_SUCCESS || iterator == null) {
System.println("Content import failed");
return;
}
System.println("Imported items: " + result[:count]);
var content = iterator.next();
while (content != null) {
System.println(content.getName());
content = iterator.next();
}
}
}Call Iterator.next() to retrieve each item. It returns null when no items remain, including when the import produces an empty iterator. An imported item can be used to launch a native app with System.exitTo() and its intent, as described in Intents.
Errors and Memory
DynamicResource.build() can throw a Lang.UnexpectedTypeException if its options are invalid. This includes missing required data or an invalid resource type or format. Unsupported resource operations may also throw this exception. For an in-memory resource, handle a null result as well as exceptions. For persisted content, check the callback's status and nullable fields.
Keep data and resource sizes within the app's available heap space. The encoded byte array and the built resource both require heap space while the resource is being created, and a decoded image may be much larger than its encoded data. Reuse built resources instead of repeatedly creating them, and release references to data and resources when they are no longer needed.