Download, save and usage of media files during runtime

Hello there,

I have two related topics concerning downloading and playing of videos and images on Android during runtime. I need to save the files in order to access them when I am offline.

Topic 1: I want to download video (mp4) and image (png) files from the internet and save them somewhere on my Android device (internal storage/ sd card) so I can access them later. Is there any way to do this in Unreal?
(Note: In Unity there is the WWW class and the Application.dataPath stuff so you are able to download and save whatever you want to your phone)

Topic 2: Afterwards I want to access the downloaded files and use them as textures (Tx2D, movie texture) to display them on 3D objects. Any idea how to load images/movie from storage/sd card as texture? For videos: How to also play the sound?
I already figured out that there is a BP node (DownloadImage) where you can pass an URL and get a texture but I also want to save it so I am able to access it when I am offline. The same applies to the video topic as well where I found a way to stream a video (set URL) but nothing to save and load from storage/sd.

Note: I have no issue dealing with C++ code.

Thanks

Hi imbakeks,

You can get the path to write to the application sandbox on Android from GExternalPath (defined in AndroidFile.cpp). Just add an extern FString GExternalPath; and access it protected with #if PLATFORM_ANDROID.

DownloadImage is implemented in AsyncTaskDownloadImage.cpp. Look in HandleImageRequest() to see how ImageWrapper is used to load the data into a texture. Just feed in the data you read from the file.

Hi Chris,

thanks for the fast reply! That pointed me into the right direction where to search inside the engine code. I’ll post an update (and maybe some code snippets) as soon as I got it all working.

Any chance of a share of code for this…
I am too trying to download mp4 to disk…
I’m a bit sketchy on C++ so not been able to get the code to work !

Hey DrunkenKBliths, I’m sorry but I didn’t implement a lot. As far as I can remember I started working on the image download but it didn’t work before the project was canceled.

Hey,** is any idea to save the download Image to local disk at runtime? I can download and save some texture already in the project folder, however, i can’t save the download image to local disk.
If anyone knows, please let me know this, i doing this for whole day.
Thanks advance!**

Hey JayChang-zhe how are you able to save in project folder

Runtime Files Downloader in Code Plugins - UE Marketplace (unrealengine.com)
i think this may help you

I figured it out, the download works well. Reply if you want to know the codes

I would like to know this. I’ve been working on a jigsaw puzzle game for a while now and just taught myself some C++ skills so I could stop depending on plugins. What code did you end up needing (or resources to look for) that helped you get downloading to work and saving to android local “disk”?

just please write it down here so we can all see it and help everyone.

Ok, so my game is in testing phase and the download system works in the Engine (ue 4.26.2) like this:

First of all you’re gonna’ need a plugin that is eventually Runtime Files Downloader in Code Plugins - UE Marketplace (unrealengine.com)

After that you’re done with all the assets need from anywhere, lets proceed!

Step 1

You’ll need to do a bit of work in this one. Gather all of the files that are to be downloaded optionally from the menu in game.

Make a folder named “After_Patch” for example, now make subfolders for each set of meshes and their respective texture and material files.
image

Now, go to each subfolders and add a “Primary Asset Label” one in each subfolders.
For adding primary asset labels right click and go to Blueprints

Then search for Primary Asset Label in BP classes.

In all of them set priority to “1”, Chunk ID to “1001” and increment it by 1 for other assets like “1002”, “1003”, “1004”, and etc. Set cook rule to “Always Cook” and Label Assets in My directory to “true”. Do all the things same

Note: if you have a sound file it will not be automatically referenced by the asset label for that you’ll have to add it in the Explicit assets, same for any blueprint file.

After that for confirming the procedure right click on the asset label and go to “Reference Viewer

Now If you have done that for all then export your game and make sure your “After Patch” folder is not added into directories to never cook section and Create cooked packages should be false in the packaging section.

Export your Package for Desired platform (if doing for android then make sure to set package game data inside apk to false) and now you will receive some extra pak files with the chunk IDs that were 1001, 1002… appended in the extra pak file’s names. In my case it is

The Step 1 is complete

Step 2

This step is about uploading the files to server to download them later via game menu.
I used onedrive if you have another way you can use that and skip to step 3.

upload all files to onedrive and you’ll get a link like “-www.dropbox.com/s/eagfsvphrjh6fvb/pakchunk1001-Android_Multi.pak?dl=0”
you just have to change the “dl=0” to “dl=1” and now this link is converted to direct download when requested by game.

Step 3

Make a list of all the links along with their pak file’s names if there are much of it and then just make some button in the menu.

In the button, first we’ll check that if the pak file is already downloaded in the device memory before if so we’ll not trigger the download event.

} for that do the following:
get project content directory → append with “paks/your_pak’s_name.pak” → query to branch with “File Exists” boolean.

And if true, for now connect a “Print string” that says file exists already

Else if false, then trigger the following events:

Download file to storage and connect url to URL and your pakchunk directory to Save Path and write “application/octet-stream” in Content type

:red_square:On Progress: this is a delegate pin create a custom event from it and now do this to get percent

try truncating the integer value to return download progress without decimal and further divide it by 100 before ceiling the value if to be used in progress bar element.

:red_square:On Complete: this is a delegate pin create a custom event from it, it will return if the download failed succeeded or other reasons of download ending.
image

And you can make different messages like this

Step 4

Now, we’re done but still before distributing the app lets finalize this system.
go to Project settings and go to Packaging and select create compressed packages and the go to Directories to never cook and add the “After Patch” there. If packaging for android go to android and set package game data inside apk to true if wanted. and we’re done for this. Enjoy

Edit 1

This downloads the file but does not unpack it automatically, If player restarts the game after installing files then the engine recognized the pack files and unpack it. But you can use paid plugins for unpacking after download, you can use the path you used for check if file is available (see Step 3 first image for reference).

2 Likes