Download Images and show them dynamically
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:
c#:
Kind regards and thanks for your help!
Thies
PS: Using Unity 2020.3.16f1 and noesis 3.1
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:
Code: Select all
<Image x:Name="m_Img" />
Code: Select all
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
Re: Download Images and show them dynamically
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.we need to download custom changed jpeg&pngs at runtime, store them locally on the device and show them by code.
Or if you have access to the image, you can also use BitmapSource::Create to fill the bits by code.
Re: Download Images and show them dynamically
Thanks for your quick answer.
I manage it to load an image, by building this helper method:
Maybe it could help somebody.
Greets,
Thies
I manage it to load an image, by building this helper method:
Code: Select all
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;
}
Greets,
Thies
Re: Download Images and show them dynamically
That's even better than my solution. Thanks for the feedback.
Re: Download Images and show them dynamically
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
does not clean up all the memory.
Do I have to dispose the TextureSource, too?
By doing this, I got a huge memory leak. And even Destroying the texture2d after no need for the TextureSource with
Code: Select all
UnityEngine.Texture2D.Destroy(texture2D);
Do I have to dispose the TextureSource, too?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Download Images and show them dynamically
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?
Could you also try to reset the property in the UI tree where the TextureSource is used?
Who is online
Users browsing this forum: No registered users and 86 guests