0

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.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • In order to disable the caching of pictures, it may be necessary to change the picture path. If pre caching is required, please set a variable to point to the path of the cached image. You may have a try first according to this idea. Or you may refer to this website:https://stackoverflow.com/questions/728616/disable-cache-for-some-images – Hongxin Sui-MSFT Aug 30 '22 at 07:36
  • @HongxinSui There is no "picture path", just a Stream from which an ImageSource is created. Note that I am not asking about HTTP caching. The caching here is performed by the imaging implementation of .NET MAUI on Android, presumably the Glide library. – Clemens Aug 30 '22 at 07:41
  • 1
    In that case, you may need to disable both hard disk cache and memory cache, please try to do like this: Glide.with(this) .load(imageUri) .skipMemoryCache(true) //disable memory cache .diskCacheStrategy(DiskCacheStrategy.NONE) //disable hard disk cache .into(imageView) And I'm just suggesting. – Hongxin Sui-MSFT Aug 30 '22 at 08:52

1 Answers1

4

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.

Line 261

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
}

Line 232

private static void loadInto(
    RequestBuilder<Drawable> builder, ImageView imageView,
    Boolean cachingEnabled, ImageLoaderCallback callback) {

    MauiCustomViewTarget target = new MauiCustomViewTarget(imageView, callback);
    prepare(builder, target, cachingEnabled, callback);
}

Line 214

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);
}
Clemens
  • 123,504
  • 12
  • 155
  • 268