简体   繁体   English

下载离线版图片并显示在 xamarin forms

[英]Download offline version of images and display in xamarin forms

in my app I would like to program an "offline version" of an display.在我的应用程序中,我想编写一个“离线版本”的显示器。 That is, you can decide in advance to download the images and later videos and then view them without mobile data (due to network coverage), for example.也就是说,您可以提前决定下载图像和稍后的视频,然后在没有移动数据(由于网络覆盖)的情况下查看它们。

I use step 1 ( Xamarin.Forms: How to download an Image, save it locally and display it on screen? ) to download the file.我使用步骤 1 ( Xamarin.Forms: How to download an Image, save it local and display it on screen? ) 下载文件。 Step 2 I can not use because the maximum data size is exceeded for larger files.第 2 步我不能使用,因为较大的文件超出了最大数据大小。 So I use PCLStorage and save the file locally.所以我使用 PCLStorage 并将文件保存在本地。

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

Now I want to display the images again, but no matter what I do, the image is not shown.现在我想再次显示图像,但无论我做什么,图像都不会显示。 I rewrite the imagesource URL to the local file.我将图像源 URL 重写为本地文件。 No matter if only with "filename.jpg" or "file:///data/user/0/com.xxx.yyy_app/files/filename.jpg" no image is shown.无论仅使用“filename.jpg”还是“file:///data/user/0/com.xxx.yyy_app/files/filename.jpg”都不会显示图像。 But I can't even check if the image was downloaded correctly as such, because the files in localstorage are not shown in a filemanager.但我什至无法检查图像是否正确下载,因为本地存储中的文件未显示在文件管理器中。 folder.CheckExistsAsync says that the file exists. folder.CheckExistsAsync 表示该文件存在。

Partly I get this error:部分我得到这个错误:

  [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\1\s\Xamarin.Forms.Core\StreamWrapper.cs:99 
  at Xamarin.Forms.Forms+AndroidPlatformServices.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x0003f] in D:\a\1\s\Xamarin.Forms.Platform.Android\Forms.cs:854 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000cf] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:136 

Long story short: I want to download an image and display it from the local file system.长话短说:我想从本地文件系统下载图像并显示它。 How do I do that?我怎么做?

Thanks a lot for the help.非常感谢您的帮助。

Translated with www.DeepL.com/Translator (free version)使用www.DeepL.com/Translator翻译(免费版)

I want to download an image and display it from the local file system.我想从本地文件系统下载图像并显示它。 How do I do that?我怎么做?

The correct steps正确的步骤

  1. Download Image from remote url.从远程 url 下载图像。

  2. Save it into disk locally.将其保存到本地磁盘。

  3. Get the steam data via the path(folder+fileName).通过路径(文件夹+文件名)获取 Steam 数据。

  4. Display it on Image.在图像上显示它。

This solution 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 , cause the later one can handle large file storage.此解决方案效果很好,但在您的场景中图像尺寸太大,因此我们只需将Xamarin.Essentials.Preferences替换为内置文件处理,因为后者可以处理大文件存储。

Modify the method SaveToDisk and GetFromDisk .修改方法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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM