Reputation: 2107
I'm trying to load big amount of images as a DataSource for GridApp template. I use recursive function to walk trough folders:
public async void walk(StorageFolder folder)
{
IReadOnlyList<StorageFolder> subDirs = null;
subDirs = await folder.GetFoldersAsync();
foreach (var subDir in subDirs)
{
await SampleDataSource.AddGroupForFolderAsync(subDir.Path);
walk(subDir);
}
}
And here is the core function:
public static async Task<bool> AddGroupForFolderAsync(string folderPath)
{
if (SampleDataSource.GetGroup(folderPath) != null) return false;
StorageFolder fd = await StorageFolder.GetFolderFromPathAsync(folderPath);
IReadOnlyList<StorageFile> fList1 = await fd.GetFilesAsync();
List<StorageFile> fList2 = new List<StorageFile>();
foreach (var file in fList1)
{
string ext = Path.GetExtension(file.Path).ToLower();
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") fList2.Add(file);
}
if (fList2.Count != 0)
{
var folderGroup = new SampleDataGroup(
uniqueId: folderPath,
title: fd.Path,
subtitle: null,
imagePath: null,
description: "Description goes here");
foreach (var i in fList2)
{
StorageFile fl = await StorageFile.GetFileFromPathAsync(i.Path);
IRandomAccessStream Stream = await fl.OpenAsync(FileAccessMode.Read);
BitmapImage pict = new BitmapImage();
pict.SetSource(Stream);
if (pict != null && folderGroup.Image == null)
{
folderGroup.SetImage(pict);
}
var dataItem = new SampleDataItem(
uniqueId: i.Path,
title: i.Path,
subtitle: null,
imagePath: pict,
description: "Decription goes here",
content: "Content goes here",
@group: folderGroup);
folderGroup.Items.Add(dataItem);
}
AllGroups.Add(folderGroup);
return true;
}
else { return false; }
}
I need to load a real big amount of files ( 164 folders and more than 1300 files, 500 MB at all). Later this amount could be bigger. It seems like IRandomAccessStream loads files into RAM. How to load images to app directly from HDD? Is it possible for Windows-Store Apps? Is it possible to do it still asynchronously?
I understand that my code needs re-factoring. I'm not asking to re-write it. I only need an advice how to save memory in this situation.
Upvotes: 0
Views: 239
Reputation: 26342
There is no way that you can load all those files, and not run into memory related problems. You need to load the file locations, map them, and possibly create a thumbnail version of the picture that you can show in your application. Then once you actually need to load the picture you unload any previous pictures and load the ones that you actually need.
When you need to look up the next file to load on the screen you simply load the picture using the location you have cached in your application.
Upvotes: 1