11using System ;
22using System . IO ;
33using UnityEngine ;
4+ #if ! UNITY_EDITOR && ( UNITY_ANDROID || UNITY_IOS )
45using NativeGalleryNamespace ;
6+ #endif
57
68public static class NativeGallery
79{
10+ public struct ImageProperties
11+ {
12+ public int width ;
13+ public int height ;
14+ public string mimeType ;
15+ }
16+
817 public enum Permission { Denied = 0 , Granted = 1 , ShouldAsk = 2 } ;
918
19+ public delegate void MediaSaveCallback ( string error ) ;
1020 public delegate void MediaPickCallback ( string path ) ;
1121 public delegate void MediaPickMultipleCallback ( string [ ] paths ) ;
12-
22+
1323#if ! UNITY_EDITOR && UNITY_ANDROID
1424 private static AndroidJavaClass m_ajc = null ;
1525 private static AndroidJavaClass AJC
@@ -41,28 +51,31 @@ private static AndroidJavaObject Context
4151 }
4252#elif ! UNITY_EDITOR && UNITY_IOS
4353 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
44- private static extern int _CheckPermission ( ) ;
54+ private static extern int _NativeGallery_CheckPermission ( ) ;
55+
56+ [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
57+ private static extern int _NativeGallery_RequestPermission ( ) ;
4558
4659 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
47- private static extern int _RequestPermission ( ) ;
60+ private static extern int _NativeGallery_CanOpenSettings ( ) ;
4861
4962 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
50- private static extern int _CanOpenSettings ( ) ;
63+ private static extern void _NativeGallery_OpenSettings ( ) ;
5164
5265 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
53- private static extern void _OpenSettings ( ) ;
66+ private static extern void _NativeGallery_ImageWriteToAlbum ( string path , string album ) ;
5467
5568 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
56- private static extern void _ImageWriteToAlbum ( string path , string album ) ;
69+ private static extern void _NativeGallery_VideoWriteToAlbum ( string path , string album ) ;
5770
5871 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
59- private static extern void _VideoWriteToAlbum ( string path , string album ) ;
72+ private static extern void _NativeGallery_PickImage ( string imageSavePath ) ;
6073
6174 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
62- private static extern void _PickImage ( string imageSavePath ) ;
75+ private static extern void _NativeGallery_PickVideo ( ) ;
6376
6477 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
65- private static extern void _PickVideo ( ) ;
78+ private static extern string _NativeGallery_GetImageProperties ( string path ) ;
6679#endif
6780
6881 public static Permission CheckPermission ( )
@@ -74,7 +87,7 @@ public static Permission CheckPermission()
7487
7588 return result ;
7689#elif ! UNITY_EDITOR && UNITY_IOS
77- return ( Permission ) _CheckPermission ( ) ;
90+ return ( Permission ) _NativeGallery_CheckPermission ( ) ;
7891#else
7992 return Permission . Granted ;
8093#endif
@@ -102,7 +115,7 @@ public static Permission RequestPermission()
102115 return ( Permission ) nativeCallback . Result ;
103116 }
104117#elif ! UNITY_EDITOR && UNITY_IOS
105- return ( Permission ) _RequestPermission ( ) ;
118+ return ( Permission ) _NativeGallery_RequestPermission ( ) ;
106119#else
107120 return Permission . Granted ;
108121#endif
@@ -111,7 +124,7 @@ public static Permission RequestPermission()
111124 public static bool CanOpenSettings ( )
112125 {
113126#if ! UNITY_EDITOR && UNITY_IOS
114- return _CanOpenSettings ( ) == 1 ;
127+ return _NativeGallery_CanOpenSettings ( ) == 1 ;
115128#else
116129 return true ;
117130#endif
@@ -122,41 +135,41 @@ public static void OpenSettings()
122135#if ! UNITY_EDITOR && UNITY_ANDROID
123136 AJC . CallStatic ( "OpenSettings" , Context ) ;
124137#elif ! UNITY_EDITOR && UNITY_IOS
125- _OpenSettings ( ) ;
138+ _NativeGallery_OpenSettings ( ) ;
126139#endif
127140 }
128141
129- public static Permission SaveImageToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted )
142+ public static Permission SaveImageToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted , MediaSaveCallback callback = null )
130143 {
131- return SaveToGallery ( mediaBytes , album , filenameFormatted , true ) ;
144+ return SaveToGallery ( mediaBytes , album , filenameFormatted , true , callback ) ;
132145 }
133146
134- public static Permission SaveImageToGallery ( string existingMediaPath , string album , string filenameFormatted )
147+ public static Permission SaveImageToGallery ( string existingMediaPath , string album , string filenameFormatted , MediaSaveCallback callback = null )
135148 {
136- return SaveToGallery ( existingMediaPath , album , filenameFormatted , true ) ;
149+ return SaveToGallery ( existingMediaPath , album , filenameFormatted , true , callback ) ;
137150 }
138151
139- public static Permission SaveImageToGallery ( Texture2D image , string album , string filenameFormatted )
152+ public static Permission SaveImageToGallery ( Texture2D image , string album , string filenameFormatted , MediaSaveCallback callback = null )
140153 {
141154 if ( image == null )
142155 throw new ArgumentException ( "Parameter 'image' is null!" ) ;
143156
144157 if ( filenameFormatted . EndsWith ( ".jpeg" ) || filenameFormatted . EndsWith ( ".jpg" ) )
145- return SaveToGallery ( image . EncodeToJPG ( 100 ) , album , filenameFormatted , true ) ;
158+ return SaveToGallery ( image . EncodeToJPG ( 100 ) , album , filenameFormatted , true , callback ) ;
146159 else if ( filenameFormatted . EndsWith ( ".png" ) )
147- return SaveToGallery ( image . EncodeToPNG ( ) , album , filenameFormatted , true ) ;
160+ return SaveToGallery ( image . EncodeToPNG ( ) , album , filenameFormatted , true , callback ) ;
148161 else
149- return SaveToGallery ( image . EncodeToPNG ( ) , album , filenameFormatted + ".png" , true ) ;
162+ return SaveToGallery ( image . EncodeToPNG ( ) , album , filenameFormatted + ".png" , true , callback ) ;
150163 }
151164
152- public static Permission SaveVideoToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted )
165+ public static Permission SaveVideoToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted , MediaSaveCallback callback = null )
153166 {
154- return SaveToGallery ( mediaBytes , album , filenameFormatted , false ) ;
167+ return SaveToGallery ( mediaBytes , album , filenameFormatted , false , callback ) ;
155168 }
156169
157- public static Permission SaveVideoToGallery ( string existingMediaPath , string album , string filenameFormatted )
170+ public static Permission SaveVideoToGallery ( string existingMediaPath , string album , string filenameFormatted , MediaSaveCallback callback = null )
158171 {
159- return SaveToGallery ( existingMediaPath , album , filenameFormatted , false ) ;
172+ return SaveToGallery ( existingMediaPath , album , filenameFormatted , false , callback ) ;
160173 }
161174
162175 public static bool CanSelectMultipleFilesFromGallery ( )
@@ -197,7 +210,7 @@ public static bool IsMediaPickerBusy()
197210#endif
198211 }
199212
200- private static Permission SaveToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted , bool isImage )
213+ private static Permission SaveToGallery ( byte [ ] mediaBytes , string album , string filenameFormatted , bool isImage , MediaSaveCallback callback )
201214 {
202215 Permission result = RequestPermission ( ) ;
203216 if ( result == Permission . Granted )
@@ -215,13 +228,13 @@ private static Permission SaveToGallery( byte[] mediaBytes, string album, string
215228
216229 File . WriteAllBytes ( path , mediaBytes ) ;
217230
218- SaveToGalleryInternal ( path , album , isImage ) ;
231+ SaveToGalleryInternal ( path , album , isImage , callback ) ;
219232 }
220233
221234 return result ;
222235 }
223236
224- private static Permission SaveToGallery ( string existingMediaPath , string album , string filenameFormatted , bool isImage )
237+ private static Permission SaveToGallery ( string existingMediaPath , string album , string filenameFormatted , bool isImage , MediaSaveCallback callback )
225238 {
226239 Permission result = RequestPermission ( ) ;
227240 if ( result == Permission . Granted )
@@ -239,23 +252,27 @@ private static Permission SaveToGallery( string existingMediaPath, string album,
239252
240253 File . Copy ( existingMediaPath , path , true ) ;
241254
242- SaveToGalleryInternal ( path , album , isImage ) ;
255+ SaveToGalleryInternal ( path , album , isImage , callback ) ;
243256 }
244257
245258 return result ;
246259 }
247260
248- private static void SaveToGalleryInternal ( string path , string album , bool isImage )
261+ private static void SaveToGalleryInternal ( string path , string album , bool isImage , MediaSaveCallback callback )
249262 {
250263#if ! UNITY_EDITOR && UNITY_ANDROID
251264 AJC . CallStatic ( "MediaScanFile" , Context , path ) ;
252265
266+ if ( callback != null )
267+ callback ( null ) ;
268+
253269 Debug . Log ( "Saving to gallery: " + path ) ;
254270#elif ! UNITY_EDITOR && UNITY_IOS
271+ NGMediaSaveCallbackiOS . Initialize ( callback ) ;
255272 if ( isImage )
256- _ImageWriteToAlbum ( path , album ) ;
273+ _NativeGallery_ImageWriteToAlbum ( path , album ) ;
257274 else
258- _VideoWriteToAlbum ( path , album ) ;
275+ _NativeGallery_VideoWriteToAlbum ( path , album ) ;
259276
260277 Debug . Log ( "Saving to Pictures: " + Path . GetFileName ( path ) ) ;
261278#endif
@@ -325,9 +342,9 @@ private static Permission GetMediaFromGallery( MediaPickCallback callback, bool
325342#elif ! UNITY_EDITOR && UNITY_IOS
326343 NGMediaReceiveCallbackiOS . Initialize ( callback ) ;
327344 if ( imageMode )
328- _PickImage ( Path . Combine ( Application . temporaryCachePath , "tmp.png" ) ) ;
345+ _NativeGallery_PickImage ( Path . Combine ( Application . temporaryCachePath , "tmp.png" ) ) ;
329346 else
330- _PickVideo ( ) ;
347+ _NativeGallery_PickVideo ( ) ;
331348#else
332349 if ( callback != null )
333350 callback ( null ) ;
@@ -373,4 +390,48 @@ private static Permission GetMultipleMediaFromGallery( MediaPickMultipleCallback
373390
374391 return result ;
375392 }
393+
394+ public static ImageProperties GetImageProperties ( string imagePath )
395+ {
396+ if ( ! File . Exists ( imagePath ) )
397+ throw new FileNotFoundException ( "File not found at " + imagePath ) ;
398+
399+ string value = null ;
400+ #if ! UNITY_EDITOR && UNITY_ANDROID
401+ value = AJC . CallStatic < string > ( "GetImageProperties" , imagePath ) ;
402+ #elif ! UNITY_EDITOR && UNITY_IOS
403+ value = _NativeGallery_GetImageProperties ( imagePath ) ;
404+ #endif
405+
406+ ImageProperties result = new ImageProperties ( ) ;
407+ if ( ! string . IsNullOrEmpty ( value ) )
408+ {
409+ string [ ] properties = value . Split ( '>' ) ;
410+ if ( properties != null && properties . Length >= 3 )
411+ {
412+ int width , height ;
413+ if ( int . TryParse ( properties [ 0 ] , out width ) )
414+ result . width = width ;
415+ if ( int . TryParse ( properties [ 1 ] , out height ) )
416+ result . height = height ;
417+
418+ if ( ! string . IsNullOrEmpty ( properties [ 2 ] ) )
419+ result . mimeType = properties [ 2 ] ;
420+ else
421+ {
422+ String extension = Path . GetExtension ( imagePath ) ;
423+ if ( extension == ".png" )
424+ result . mimeType = "image/png" ;
425+ else if ( extension == ".jpg" || extension == ".jpeg" )
426+ result . mimeType = "image/jpeg" ;
427+ else if ( extension == ".gif" )
428+ result . mimeType = "image/gif" ;
429+ else if ( extension == ".bmp" )
430+ result . mimeType = "image/bmp" ;
431+ }
432+ }
433+ }
434+
435+ return result ;
436+ }
376437}
0 commit comments