下载离线版本的图像并以 xamarin 形式显示

Download offline version of images and display in xamarin forms

在我的应用程序中,我想编写一个显示的“离线版本”。也就是说,您可以提前决定下载图片和稍后的视频,然后在没有移动数据(由于网络覆盖)的情况下查看它们,例如。

我使用步骤 1 () 下载文件。 第 2 步我无法使用,因为较大的文件超出了最大数据大小。所以我使用 PCLStorage 并在本地保存文件。

IFolder folder = FileSystem.Current.LocalStorage;
IFile file = await folder.CreateFileAsync(imageFileName, CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync(Convert.ToBase64String(imageAsBase64String));

现在我想重新显示图片,但是怎么都显示不出来。 我将图像源 URL 重写到本地文件。无论只用“filename.jpg”还是“file:///data/user/0/com.xxx.yyy_app/files/filename.jpg”都不会显示图像。 但我什至无法检查图像是否已正确下载,因为本地存储中的文件未显示在文件管理器中。 folder.CheckExistsAsync 表示文件存在。

部分我得到这个错误:

  [0:] Image Loading: Error getting stream for file:///data/user/0/com.xxx.yyy_app/files/2_76_1.jpg: System.InvalidOperationException: 
An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.
  at System.Net.Http.HttpClient.PrepareRequestMessage (System.Net.Http.HttpRequestMessage request) [0x0008a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:678 
  at System.Net.Http.HttpClient.SendAsync (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x00020] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:437 
  at System.Net.Http.HttpClient.GetAsync (System.Uri requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:311 
  at System.Net.Http.HttpClient.GetAsync (System.Uri requestUri, System.Threading.CancellationToken cancellationToken) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:299 
  at Xamarin.Forms.StreamWrapper.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.Http.HttpClient client) [0x00011] in D:\a\s\Xamarin.Forms.Core\StreamWrapper.cs:99 
  at Xamarin.Forms.Forms+AndroidPlatformServices.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x0003f] in D:\a\s\Xamarin.Forms.Platform.Android\Forms.cs:854 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000cf] in D:\a\s\Xamarin.Forms.Core\UriImageSource.cs:136 

长话短说:我想下载一张图片并从本地文件系统显示它。我该怎么做?

非常感谢您的帮助。

www.DeepL.com/Translator 翻译(免费版)

I want to download an image and display it from the local file system. How do I do that?

正确的步骤

  1. 从远程下载图像 url .

  2. 保存到本地磁盘。

  3. 通过路径(文件夹+文件名)获取steam数据。

  4. 在图像上显示。

works well but in your scenario the difference that the image size is too big , so we just need to replace Xamarin.Essentials.Preferences with built-in File Handling,因为后者可以处理大文件存储。

修改方法 SaveToDiskGetFromDisk .

string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

public static void SaveToDisk(string imageFileName, byte[] imageAsBase64String)
{  
    var fileName = Path.Combine(folder ,imageFileName);
    File.WriteAllText(fileName , Convert.ToBase64String(imageAsBase64String));
}

public static Xamarin.Forms.ImageSource GetFromDisk(string imageFileName)
{
    var fileName = Path.Combine(folder ,imageFileName);
    var imageAsBase64String = File.ReadAllText(fileName)
    return ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(imageAsBase64String)));
}