When I load an ImageSource
from an Uri
, I can disable image caching by setting the CachingEnabled
property to false.
With an Image element in XAML
<Image x:Name="image"/>
the code below will load the image without any caching.
var imageUri = "https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
image.Source = new UriImageSource
{
Uri = new Uri(imageUri),
CachingEnabled = false
};
Now I want to do the same with a StreamImageSource
:
var httpClient = new HttpClient();
image.Source = new StreamImageSource
{
Stream = async cancellationToken =>
{
var response = await httpClient.GetAsync(imageUri, cancellationToken);
return await response.Content.ReadAsStreamAsync(cancellationToken);
}
};
There seems to be no property to enable or disable caching, and I would not expect any caching at all. But on my Android device, the application writes image cache files to a folder named image_manager_disk_cache
in FileSystem.CacheDirectory
, which can be reproduced by
var imageCacheDirectory = Path.Combine(
FileSystem.CacheDirectory, "image_manager_disk_cache");
if (Directory.Exists(imageCacheDirectory))
{
foreach (var imageCacheFile in Directory.EnumerateFiles(imageCacheDirectory))
{
Debug.WriteLine(imageCacheFile);
}
}
Sample output is:
[0:] /data/user/0/com.companyname.imagecachetest/cache/image_manager_disk_cache/journal
[0:] /data/user/0/com.companyname.imagecachetest/cache/image_manager_disk_cache/5bcc99fe6930c85add40cfc486ae9a5a0451c08d71c6d3cd679ea26ec0293b9b.0
How can I disable this image caching for StreamImageSource?
Edit: My guess is that this caching is performed by the Glide
library, which is used by the imaging implementation of .NET MAUI on Android. I could however not find any information about how to disable Glide image caching in a MAUI application.
Update: It seems to be fixed now, at least for StreamImageSource
. See here: https://github.com/dotnet/maui/pull/13111
Looking through the .NET MAUI source code I found
src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/PlatformInterop.java
with relevant methods shown below.
In loadImageFromStream
, the cachingEnabled
argument is always set to true
. This seems wrong to me. I have created an issue on GitHub: https://github.com/dotnet/maui/issues/9773.
public static void loadImageFromStream(
ImageView imageView, InputStream inputStream,
ImageLoaderCallback callback) {
RequestBuilder<Drawable> builder = Glide
.with(imageView)
.load(inputStream);
loadInto(builder, imageView, true, callback); // true seems wrong here
}
private static void loadInto(
RequestBuilder<Drawable> builder, ImageView imageView,
Boolean cachingEnabled, ImageLoaderCallback callback) {
MauiCustomViewTarget target = new MauiCustomViewTarget(imageView, callback);
prepare(builder, target, cachingEnabled, callback);
}
private static void prepare(
RequestBuilder<Drawable> builder, Target<Drawable> target,
Boolean cachingEnabled, ImageLoaderCallback callback) {
builder = builder
.error(callback);
if (!cachingEnabled) {
builder = builder
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
}
builder
.into(target);
}