Skip to content

Commit f0f9b2a

Browse files
committed
Added callback to save functions + added GetImageProperties function + added prefix to iOS function names
Special thanks to @baado yasirkula#15
1 parent 225357b commit f0f9b2a

12 files changed

+266
-96
lines changed

JAR Source/NativeGallery.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import android.content.Context;
88
import android.content.Intent;
99
import android.content.pm.PackageManager;
10+
import android.graphics.BitmapFactory;
1011
import android.media.MediaScannerConnection;
1112
import android.net.Uri;
1213
import android.os.Build;
1314
import android.os.Environment;
1415
import android.provider.MediaStore;
1516
import android.provider.Settings;
17+
import android.util.Log;
1618

1719
import java.io.File;
1820

@@ -118,4 +120,26 @@ public static boolean CanSelectMultipleMedia()
118120
{
119121
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
120122
}
123+
124+
// Credit: https://stackoverflow.com/a/16440953/2373034
125+
public static String GetImageProperties( String path )
126+
{
127+
try
128+
{
129+
BitmapFactory.Options options = new BitmapFactory.Options();
130+
options.inJustDecodeBounds = true;
131+
BitmapFactory.decodeFile( path, options );
132+
133+
String mimeType = options.outMimeType;
134+
if( mimeType == null )
135+
mimeType = "";
136+
137+
return options.outWidth + ">" + options.outHeight + ">" + mimeType;
138+
}
139+
catch( Exception e )
140+
{
141+
Log.e( "Unity", "Exception:", e );
142+
return "";
143+
}
144+
}
121145
}

NativeGallery.unitypackage

1.01 KB
Binary file not shown.

Plugins/NativeGallery/Android/NGMediaReceiveCallbackAndroid.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Threading;
1+
#if !UNITY_EDITOR && UNITY_ANDROID
2+
using System.Threading;
23
using UnityEngine;
34

45
namespace NativeGalleryNamespace
56
{
6-
public class NGMediaReceiveCallbackAndroid
7-
#if UNITY_ANDROID
8-
: AndroidJavaProxy
7+
public class NGMediaReceiveCallbackAndroid : AndroidJavaProxy
98
{
109
private object threadLock;
1110

@@ -66,7 +65,5 @@ public void OnMultipleMediaReceived( string paths )
6665
}
6766
}
6867
}
69-
#else
70-
{ }
71-
#endif
72-
}
68+
}
69+
#endif

Plugins/NativeGallery/Android/NGPermissionCallbackAndroid.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Threading;
1+
#if !UNITY_EDITOR && UNITY_ANDROID
2+
using System.Threading;
23
using UnityEngine;
34

45
namespace NativeGalleryNamespace
56
{
6-
public class NGPermissionCallbackAndroid
7-
#if UNITY_ANDROID
8-
: AndroidJavaProxy
7+
public class NGPermissionCallbackAndroid : AndroidJavaProxy
98
{
109
private object threadLock;
1110
public int Result { get; private set; }
@@ -26,7 +25,5 @@ public void OnPermissionResult( int result )
2625
}
2726
}
2827
}
29-
#else
30-
{ }
31-
#endif
32-
}
28+
}
29+
#endif
428 Bytes
Binary file not shown.

Plugins/NativeGallery/Android/NativeGallery.jar.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Plugins/NativeGallery/NativeGallery.cs

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
using System;
22
using System.IO;
33
using UnityEngine;
4+
#if !UNITY_EDITOR && ( UNITY_ANDROID || UNITY_IOS )
45
using NativeGalleryNamespace;
6+
#endif
57

68
public 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

Comments
 (0)