Download image and store locally and sercurely

Gordon S 501 Reputation points
2022-03-30T10:41:42.897+00:00

Our app displays a number of different "products" and an image for each. There are quite a large number of possible products / images and is not a fixed set.

We already get the list of products that a user has from our web server via our API, however we currently just use a generic image for each. I could add the product images to the project and embed them into the app, but that doesn't seem like the best or most flexible solution.

I could retrieve the image for each product, when it is displayed, via our API but I am concerned about the speed of download.

So, I thought about having local storage for the images where images are stored after getting them from the API. So, when an image is required by the app it checks the local storage, if it isn't there it gets it from the API and then stores it locally for future use. Then, only the first retrieval will be slow(er).

I'm struggling to find information on where images would be stored on the users device (phone) and if it would be secure. By secure, I mean not accessible by anyone or any other app other than ours. Can I just store them as files, or would I need to use a database?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,306 Reputation points Microsoft Vendor
    2022-03-31T06:55:16.543+00:00

    Hello,

    I thought about having local storage for the images where images are stored after getting them from the API

    You could check the Download images part of this Image doc : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=macos#download-images
    And in the source code, we can see the image has been downloaded and been written in ImageLoaderCache folder. You could refer to this solution and store the image in the path you want to store. Pay attention to replacing Device.PlatformServices.GetUserStoreForApplication(); by Xamarin.Essentials: File System Helpers.

    I mean not accessible by anyone or any other app other than ours.

    For iOS, the Application Sandbox rule is secure. It cannot access another application’s files, these files also cannot be accessed by other applications unless you share the files by Files App. For more details, refer to File system access in Xamarin.iOS.
    For Android, if you are using Internal Storage, it is secure. The files can be accessed only by the application or the operating system. For more details, refer to File Storage and Access with Xamarin.Android.

    Can I just store them as files, or would I need to use a database?

    You can store the file, and name each image with the user ID or other fields or other rules that you can query, which is convenient when reading from a path. It's not very necessary to use a database to store the image, because the image size is a little big. Usually, we use SQLite as the Local Database, it's small and lightweight. But you could save the path or URL or some other user information by database, it depends on your needs.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.