root / GES_PAC / Platforms / Android / AndroidFileHelper.cs @ 9601eaf0
Historique | Voir | Annoter | Télécharger (2,229 ko)
1 |
using Android.Content; |
---|---|
2 |
using Android.Provider; |
3 |
using System.Text; |
4 |
|
5 |
|
6 |
public static class AndroidFileHelper |
7 |
{ |
8 |
public static void SaveJsonToDownloads(string fileName, string content) |
9 |
{ |
10 |
var context = Android.App.Application.Context; |
11 |
|
12 |
ContentValues values = new ContentValues(); |
13 |
values.Put(MediaStore.IMediaColumns.DisplayName, fileName); |
14 |
values.Put(MediaStore.IMediaColumns.MimeType, "text/json"); |
15 |
|
16 |
values.Put(MediaStore.IMediaColumns.RelativePath, $"{Android.OS.Environment.DirectoryDownloads}/GES_PAC"); |
17 |
|
18 |
var uri = context.ContentResolver.Insert(MediaStore.Downloads.ExternalContentUri, values); |
19 |
if (uri == null) throw new IOException("Impossible de créer le fichier."); |
20 |
|
21 |
using var stream = context.ContentResolver.OpenOutputStream(uri); |
22 |
using var writer = new StreamWriter(stream, Encoding.UTF8); |
23 |
writer.Write(content); |
24 |
writer.Flush(); |
25 |
} |
26 |
public static void SaveCSVToDownloads(string folderName, string[] contents) |
27 |
{ |
28 |
if (contents.Length != 3) |
29 |
throw new ArgumentException("Le tableau de contenu doit contenir exactement 3 éléments."); |
30 |
|
31 |
var context = Android.App.Application.Context; |
32 |
|
33 |
string[] fileNames = { "mesures.csv", "comportements.csv", "calibrations.csv" }; |
34 |
string folderPath = $"{Android.OS.Environment.DirectoryDownloads}/GES_PAC/{folderName.Replace("/","_")}"; |
35 |
|
36 |
for (int i = 0; i < fileNames.Length; i++) |
37 |
{ |
38 |
ContentValues values = new ContentValues(); |
39 |
values.Put(MediaStore.IMediaColumns.DisplayName, fileNames[i]); |
40 |
values.Put(MediaStore.IMediaColumns.MimeType, "text/csv"); |
41 |
values.Put(MediaStore.IMediaColumns.RelativePath, folderPath); |
42 |
|
43 |
var uri = context.ContentResolver.Insert(MediaStore.Downloads.ExternalContentUri, values); |
44 |
if (uri == null) throw new IOException($"Impossible de créer le fichier {fileNames[i]}."); |
45 |
|
46 |
using var stream = context.ContentResolver.OpenOutputStream(uri); |
47 |
using var writer = new StreamWriter(stream, Encoding.UTF8); |
48 |
foreach (var line in contents[i]) |
49 |
{ |
50 |
writer.Write(line); |
51 |
} |
52 |
writer.Flush(); |
53 |
} |
54 |
} |
55 |
} |
56 |
|