User avatar
gemelo
Topic Author
Posts: 8
Joined: 12 Feb 2018, 09:43
Contact:

Download Images and show them dynamically

26 Aug 2021, 17:39

Hi,

we need to download custom changed jpeg&pngs at runtime, store them locally on the device and show them by code.

I get problems with the last point:

- File.Exist(filename) is true.
- my noesis demo app, running with visual studio without unity has no problems. Images appeared.

Problems starts when running in unity playmode and on my android device:

[noesis] Image not found '../../Resources/Images/Fundort.png'
UnityEngine.Debug:LogError (object,UnityEngine.Object)
NoesisUnity:UnityLog (int,string) (at C:/Users/Thies/Downloads/NoesisGUI-Unity-3.1.0/Runtime/NoesisUnity.cs:288)
Noesis.BitmapImage:set_UriSource (System.Uri) (at C:/Users/Thies/Downloads/NoesisGUI-Unity-3.1.0/Runtime/API/Proxies/BitmapImage.cs:51)
Noesis.BitmapImage:.ctor (System.Uri) (at C:/Users/Thies/Downloads/NoesisGUI-Unity-3.1.0/Runtime/API/Proxies/BitmapImage.cs:31)

Can you please help me, telling me, what I doing wrong?

My code is simple as it could be:

xaml:
<Image x:Name="m_Img" />
c#:
if (File.Exists(imageData.FilePath)) // <= is true
{
   System.Uri fileUri = new System.Uri(imageData.FilePath);
   m_Img.Source = new BitmapImage(fileUri); // <= no exception, but Unity throws a message log
}

Kind regards and thanks for your help!

Thies

PS: Using Unity 2020.3.16f1 and noesis 3.1
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Download Images and show them dynamically

26 Aug 2021, 18:04

we need to download custom changed jpeg&pngs at runtime, store them locally on the device and show them by code.
I am not sure there is an easy way to do this with Unity, because by default we don't access the filesystem, we are using asset references. So the proper solution to this would be using bundles or addressables.

Or if you have access to the image, you can also use BitmapSource::Create to fill the bits by code.
 
User avatar
gemelo
Topic Author
Posts: 8
Joined: 12 Feb 2018, 09:43
Contact:

Re: Download Images and show them dynamically

26 Aug 2021, 19:27

Thanks for your quick answer.

I manage it to load an image, by building this helper method:
 
public static BitmapSource CreateFromLocalFile(string localFilePath)
{
   byte[] buffer = File.ReadAllBytes(localFilePath);

   Texture2D tex = new Texture2D(2, 2);   
   tex.LoadImage(buffer);

   TextureSource textureSource = new TextureSource(tex);

   return textureSource;
}
        
Maybe it could help somebody.

Greets,

Thies
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Download Images and show them dynamically

26 Aug 2021, 19:34

That's even better than my solution. Thanks for the feedback.
 
User avatar
gemelo
Topic Author
Posts: 8
Joined: 12 Feb 2018, 09:43
Contact:

Re: Download Images and show them dynamically

26 Aug 2021, 22:48

unfortunately marked as solve too early...

By doing this, I got a huge memory leak. And even Destroying the texture2d after no need for the TextureSource with
UnityEngine.Texture2D.Destroy(texture2D);
does not clean up all the memory.

Do I have to dispose the TextureSource, too?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Download Images and show them dynamically

27 Aug 2021, 10:24

The TextureSource keeps a reference to the Texture2D, and if you are using that TextureSource in the UI tree it will be kept alive. But as soon as the TextureSource is removed from the UI tree it should be released (when GC kicks in). If that is not happening the problem could probably be with the UI elements not being released. Are you following the indications for weak events?
Could you also try to reset the property in the UI tree where the TextureSource is used?

Who is online

Users browsing this forum: Google [Bot], vinick and 81 guests