Search code examples
uno-platform

How to get uri to included asset in Uno Platform?


For integrating with a android binding project I need to provide a Uri pointing to a .zip file, which I have included in the assets and designated as AndroidAsset. How do I get the Uri for such a file? I already tried ms-appx:///Assets/file.zip and file:///Assets/file.zip

Update: Import to note is, that the function consuming the Uri is Android native code, so I suspect that ms-appx:// doesn't get resolved properly.

Update2: It is not possible to provide a stream. The method I am calling is shown in the sample here: https://github.com/Laerdal/Xamarin.Nordic.DFU.Android/blob/7244627c09e97e05ee2c8e05744f19055981486b/Sample/Nordic/FirmwareUpdater.cs#L27.

_dfuServiceInitiator.SetZip(firmwareZipFile);

The native implementation is shown here: https://github.com/NordicSemiconductor/Android-DFU-Library/blob/07bdaa50cfc5786790bf1ac589b14931de65d099/dfu/src/main/java/no/nordicsemi/android/dfu/DfuServiceInitiator.java#L620

public DfuServiceInitiator setZip(@NonNull final Uri uri) {
    return init(uri, null, 0, DfuBaseService.TYPE_AUTO, DfuBaseService.MIME_TYPE_ZIP);
}

Solution

  • Files read from the StorageFile.GetFileFromApplicationUriAsync() method may not return a stable path on UWP, and this is particularly the case on particularly on Android or WebAssembly, where the file is not necessarily on the file system.

    On Android, the file is a Stream directly built from the APK file, and on WebAssembly it is stored in a temporary location.

    In order to use keep a stable copy of the file, use the following:

    var file = await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///TextFile.txt"));
    var newFile = await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting);
    
    var txt = await FileIO.ReadTextAsync(newFile);