33using HannerLabApp . Services . Media ;
44using Plugin . CurrentActivity ;
55using System ;
6+ using System . Threading . Tasks ;
7+ using Xamarin . Essentials ;
8+ using System . IO ;
9+ using Android . Graphics ;
10+ using HannerLabApp . Extensions ;
11+ using Path = System . IO . Path ;
612
713[ assembly: Xamarin . Forms . Dependency ( typeof ( MediaService ) ) ]
814namespace HannerLabApp . Droid . Services
915{
1016 public class MediaService : IMediaService
1117 {
1218 Context CurrentContext => CrossCurrentActivity . Current . Activity ;
13- public void SaveImageFromByte ( byte [ ] imageByte , string fileName )
19+
20+ public async Task SaveImageFileResultToGalleryAsync ( FileResult file )
1421 {
1522 try
1623 {
24+ var fileName = file . FileName ;
25+
1726 var storagePath = Android . OS . Environment . GetExternalStoragePublicDirectory ( Android . OS . Environment . DirectoryPictures ) . AbsolutePath ;
18- string path = System . IO . Path . Combine ( storagePath , fileName ) ;
19- System . IO . File . WriteAllBytes ( path , imageByte ) ;
27+ string path = Path . Combine ( storagePath , fileName ) ;
28+
29+ using ( var stream = await file . OpenReadAsync ( ) )
30+ {
31+ using ( var fileStream = new FileStream ( path , FileMode . Create , FileAccess . Write ) )
32+ {
33+ stream . CopyTo ( fileStream ) ;
34+ }
35+ }
36+
2037 var mediaScanIntent = new Intent ( Intent . ActionMediaScannerScanFile ) ;
2138 mediaScanIntent . SetData ( Android . Net . Uri . FromFile ( new Java . IO . File ( path ) ) ) ;
2239 CurrentContext . SendBroadcast ( mediaScanIntent ) ;
@@ -26,5 +43,42 @@ public void SaveImageFromByte(byte[] imageByte, string fileName)
2643 Console . WriteLine ( "Failed to save image to gallery" , ex ) ;
2744 }
2845 }
46+
47+ public async Task < byte [ ] > GenerateImageThumbnailAsync ( FileResult file )
48+ {
49+ using ( var stream = await file . OpenReadAsync ( ) )
50+ {
51+ using ( var memoryStream = new MemoryStream ( ) )
52+ {
53+ await stream . CopyToAsync ( memoryStream ) ;
54+ byte [ ] bytes = memoryStream . ToArray ( ) ;
55+
56+ if ( bytes . Length <= 0 ) return null ;
57+
58+ return await ResizeImageAndroid ( bytes , 0.5f , 90 ) ;
59+ }
60+ }
61+ }
62+
63+
64+ public static async Task < byte [ ] > ResizeImageAndroid ( byte [ ] imageData , float scaleFactor , int quality )
65+ {
66+ // Load the bitmap
67+ Bitmap originalImage = BitmapFactory . DecodeByteArray ( imageData , 0 , imageData . Length ) ;
68+
69+ float oldWidth = ( float ) originalImage . Width ;
70+ float oldHeight = ( float ) originalImage . Height ;
71+
72+ float newHeight = oldHeight * scaleFactor ;
73+ float newWidth = oldWidth * scaleFactor ;
74+
75+ Bitmap resizedImage = Bitmap . CreateScaledBitmap ( originalImage , ( int ) newWidth , ( int ) newHeight , false ) ;
76+
77+ using ( MemoryStream ms = new MemoryStream ( ) )
78+ {
79+ await resizedImage . CompressAsync ( Bitmap . CompressFormat . Jpeg , quality , ms ) ;
80+ return ms . ToArray ( ) ;
81+ }
82+ }
2983 }
3084}
0 commit comments