-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlbumHelper.cs
114 lines (94 loc) · 3.63 KB
/
AlbumHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;
namespace PhotoLibraryApp
{
class AlbumHelper
{
// Global collection of pictures
public static ObservableCollection<AlbumHelper> Album = new ObservableCollection<AlbumHelper>();
//public static Collection<ObservableCollection<AlbumPic>> Albums = new Collection<ObservableCollection<AlbumPic>>();
// Path of the picture file
public string Path1 { get; set; }
// BitmapImage to be used as the souce of the image control
public BitmapImage ImageSource1 { get; set; }
// Album name
public string AlbumName { get; set; }
/// <summary>
/// Adds pictures to an albumy and updates storage file
/// </summary>
/// <param name="picture"></param>
public static async Task AddPictures(IReadOnlyList<StorageFile> storageFiles, string textFileName)
{
foreach (var storageFile in storageFiles)
{
// Add picture to the global collection
await AddPictureToAlbum(storageFile.Path, textFileName);
// Save picture file path in storage data file
FileHelper.WriteTextFileAsync(textFileName, storageFile.Path);
}
}
private static async Task AddPictureToAlbum(string filePath, string textFileName)
{
var name = textFileName.Split('.');
StorageFile storageFile = null;
try
{
storageFile = await StorageFile.GetFileFromPathAsync(filePath);
}
catch (UnauthorizedAccessException)
{
throw new Exception("Access denied to the folder");
}
// Create a bitmap
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
bitmapImage.SetSource(stream);
}
//ObservableCollection<AlbumPic> Album = new ObservableCollection<AlbumPic>();
// Create Picture object
var pic = new AlbumHelper();
pic.Path1 = storageFile.Path;
pic.ImageSource1 = bitmapImage;
pic.AlbumName = name[0];
//Add picture to album
Album.Add(pic);
// Add Album object to the global observable collection
// Albums.Add(Album);
}
public async static Task LoadPicturesFromAlbums()
{
var storageFolder = ApplicationData.Current.LocalFolder;
IReadOnlyList<StorageFile> textFiles = await storageFolder.GetFilesAsync();
foreach (var textFile in textFiles)
{
if (textFile.Name != "library.txt")
{
await LoadAllPicturesAsync(textFile.Name);
}
}
}
public async static Task LoadAllPicturesAsync(string textFileName)
{
var content = await FileHelper.ReadTextFileAsync(textFileName);
if (!string.IsNullOrWhiteSpace(content))
{
var fileList = content.Split();
foreach (string filePath in fileList)
{
if (string.IsNullOrEmpty(filePath))
{
continue;
}
await AddPictureToAlbum(filePath, textFileName);
}
}
}
}
}