NativeGallery.cs 28.1 KB
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
using System;
using System.IO;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_ANDROID || UNITY_IOS
using NativeGalleryNamespace;
#endif

public static class NativeGallery
{
	public struct ImageProperties
	{
		public readonly int width;
		public readonly int height;
		public readonly string mimeType;
		public readonly ImageOrientation orientation;

		public ImageProperties( int width, int height, string mimeType, ImageOrientation orientation )
		{
			this.width = width;
			this.height = height;
			this.mimeType = mimeType;
			this.orientation = orientation;
		}
	}

	public struct VideoProperties
	{
		public readonly int width;
		public readonly int height;
		public readonly long duration;
		public readonly float rotation;

		public VideoProperties( int width, int height, long duration, float rotation )
		{
			this.width = width;
			this.height = height;
			this.duration = duration;
			this.rotation = rotation;
		}
	}

	public enum PermissionType { Read = 0, Write = 1 };
	public enum Permission { Denied = 0, Granted = 1, ShouldAsk = 2 };

	[Flags]
	public enum MediaType { Image = 1, Video = 2, Audio = 4 };

	// EXIF orientation: http://sylvana.net/jpegcrop/exif_orientation.html (indices are reordered)
	public enum ImageOrientation { Unknown = -1, Normal = 0, Rotate90 = 1, Rotate180 = 2, Rotate270 = 3, FlipHorizontal = 4, Transpose = 5, FlipVertical = 6, Transverse = 7 };

	public delegate void MediaSaveCallback( bool success, string path );
	public delegate void MediaPickCallback( string path );
	public delegate void MediaPickMultipleCallback( string[] paths );

	#region Platform Specific Elements
#if !UNITY_EDITOR && UNITY_ANDROID
	private static AndroidJavaClass m_ajc = null;
	private static AndroidJavaClass AJC
	{
		get
		{
			if( m_ajc == null )
				m_ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeGallery" );

			return m_ajc;
		}
	}

	private static AndroidJavaObject m_context = null;
	private static AndroidJavaObject Context
	{
		get
		{
			if( m_context == null )
			{
				using( AndroidJavaObject unityClass = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ) )
				{
					m_context = unityClass.GetStatic<AndroidJavaObject>( "currentActivity" );
				}
			}

			return m_context;
		}
	}
#elif !UNITY_EDITOR && UNITY_IOS
	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern int _NativeGallery_CheckPermission( int readPermission, int permissionFreeMode );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern int _NativeGallery_RequestPermission( int readPermission, int permissionFreeMode );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern void _NativeGallery_ShowLimitedLibraryPicker();

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern int _NativeGallery_CanOpenSettings();

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern void _NativeGallery_OpenSettings();

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern int _NativeGallery_CanPickMultipleMedia();

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern int _NativeGallery_GetMediaTypeFromExtension( string extension );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern void _NativeGallery_ImageWriteToAlbum( string path, string album, int permissionFreeMode );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern void _NativeGallery_VideoWriteToAlbum( string path, string album, int permissionFreeMode );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern void _NativeGallery_PickMedia( string mediaSavePath, int mediaType, int permissionFreeMode, int selectionLimit );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern string _NativeGallery_GetImageProperties( string path );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern string _NativeGallery_GetVideoProperties( string path );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern string _NativeGallery_GetVideoThumbnail( string path, string thumbnailSavePath, int maxSize, double captureTimeInSeconds );

	[System.Runtime.InteropServices.DllImport( "__Internal" )]
	private static extern string _NativeGallery_LoadImageAtPath( string path, string temporaryFilePath, int maxSize );
#endif

#if !UNITY_EDITOR && ( UNITY_ANDROID || UNITY_IOS )
	private static string m_temporaryImagePath = null;
	private static string TemporaryImagePath
	{
		get
		{
			if( m_temporaryImagePath == null )
			{
				m_temporaryImagePath = Path.Combine( Application.temporaryCachePath, "tmpImg" );
				Directory.CreateDirectory( Application.temporaryCachePath );
			}

			return m_temporaryImagePath;
		}
	}

	private static string m_selectedMediaPath = null;
	private static string SelectedMediaPath
	{
		get
		{
			if( m_selectedMediaPath == null )
			{
				m_selectedMediaPath = Path.Combine( Application.temporaryCachePath, "pickedMedia" );
				Directory.CreateDirectory( Application.temporaryCachePath );
			}

			return m_selectedMediaPath;
		}
	}
#endif
	#endregion

	#region Runtime Permissions
	// PermissionFreeMode was initially planned to be a toggleable setting on iOS but it has its own issues when set to false, so its value is forced to true.
	// These issues are:
	// - Presented permission dialog will have a "Select Photos" option on iOS 14+ but clicking it will freeze and eventually crash the app (I'm guessing that
	//   this is caused by how permissions are handled synchronously in NativeGallery)
	// - While saving images/videos to Photos, iOS 14+ users would see the "Select Photos" option (which is irrelevant in this context, hence confusing) and
	//   the user must grant full Photos access in order to save the image/video to a custom album
	// The only downside of having PermissionFreeMode = true is that, on iOS 14+, images/videos will be saved to the default Photos album rather than the
	// provided custom album
	private const bool PermissionFreeMode = true;

	public static Permission CheckPermission( PermissionType permissionType )
	{
#if !UNITY_EDITOR && UNITY_ANDROID
		Permission result = (Permission) AJC.CallStatic<int>( "CheckPermission", Context, permissionType == PermissionType.Read );
		if( result == Permission.Denied && (Permission) PlayerPrefs.GetInt( "NativeGalleryPermission", (int) Permission.ShouldAsk ) == Permission.ShouldAsk )
			result = Permission.ShouldAsk;

		return result;
#elif !UNITY_EDITOR && UNITY_IOS
		// result == 3: LimitedAccess permission on iOS, no need to handle it when PermissionFreeMode is set to true
		int result = _NativeGallery_CheckPermission( permissionType == PermissionType.Read ? 1 : 0, PermissionFreeMode ? 1 : 0 );
		return result == 3 ? Permission.Granted : (Permission) result;
#else
		return Permission.Granted;
#endif
	}

	public static Permission RequestPermission( PermissionType permissionType )
	{
#if !UNITY_EDITOR && UNITY_ANDROID
		object threadLock = new object();
		lock( threadLock )
		{
			NGPermissionCallbackAndroid nativeCallback = new NGPermissionCallbackAndroid( threadLock );

			AJC.CallStatic( "RequestPermission", Context, nativeCallback, permissionType == PermissionType.Read, PlayerPrefs.GetInt( "NativeGalleryPermission", (int) Permission.ShouldAsk ) );

			if( nativeCallback.Result == -1 )
				System.Threading.Monitor.Wait( threadLock );

			if( (Permission) nativeCallback.Result != Permission.ShouldAsk && PlayerPrefs.GetInt( "NativeGalleryPermission", -1 ) != nativeCallback.Result )
			{
				PlayerPrefs.SetInt( "NativeGalleryPermission", nativeCallback.Result );
				PlayerPrefs.Save();
			}

			return (Permission) nativeCallback.Result;
		}
#elif !UNITY_EDITOR && UNITY_IOS
		// result == 3: LimitedAccess permission on iOS, no need to handle it when PermissionFreeMode is set to true
		int result = _NativeGallery_RequestPermission( permissionType == PermissionType.Read ? 1 : 0, PermissionFreeMode ? 1 : 0 );
		return result == 3 ? Permission.Granted : (Permission) result;
#else
		return Permission.Granted;
#endif
	}

	// This function isn't needed when PermissionFreeMode is set to true
	private static void TryExtendLimitedAccessPermission()
	{
		if( IsMediaPickerBusy() )
			return;

#if !UNITY_EDITOR && UNITY_IOS
		_NativeGallery_ShowLimitedLibraryPicker();
#endif
	}

	public static bool CanOpenSettings()
	{
#if !UNITY_EDITOR && UNITY_IOS
		return _NativeGallery_CanOpenSettings() == 1;
#else
		return true;
#endif
	}

	public static void OpenSettings()
	{
#if !UNITY_EDITOR && UNITY_ANDROID
		AJC.CallStatic( "OpenSettings", Context );
#elif !UNITY_EDITOR && UNITY_IOS
		_NativeGallery_OpenSettings();
#endif
	}
	#endregion

	#region Save Functions
	public static Permission SaveImageToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( mediaBytes, album, filename, MediaType.Image, callback );
	}

	public static Permission SaveImageToGallery( string existingMediaPath, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( existingMediaPath, album, filename, MediaType.Image, callback );
	}

	public static Permission SaveImageToGallery( Texture2D image, string album, string filename, MediaSaveCallback callback = null )
	{
		if( image == null )
			throw new ArgumentException( "Parameter 'image' is null!" );

		if( filename.EndsWith( ".jpeg", StringComparison.OrdinalIgnoreCase ) || filename.EndsWith( ".jpg", StringComparison.OrdinalIgnoreCase ) )
			return SaveToGallery( GetTextureBytes( image, true ), album, filename, MediaType.Image, callback );
		else if( filename.EndsWith( ".png", StringComparison.OrdinalIgnoreCase ) )
			return SaveToGallery( GetTextureBytes( image, false ), album, filename, MediaType.Image, callback );
		else
			return SaveToGallery( GetTextureBytes( image, false ), album, filename + ".png", MediaType.Image, callback );
	}

	public static Permission SaveVideoToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( mediaBytes, album, filename, MediaType.Video, callback );
	}

	public static Permission SaveVideoToGallery( string existingMediaPath, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( existingMediaPath, album, filename, MediaType.Video, callback );
	}

	private static Permission SaveAudioToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( mediaBytes, album, filename, MediaType.Audio, callback );
	}

	private static Permission SaveAudioToGallery( string existingMediaPath, string album, string filename, MediaSaveCallback callback = null )
	{
		return SaveToGallery( existingMediaPath, album, filename, MediaType.Audio, callback );
	}
	#endregion

	#region Load Functions
	public static bool CanSelectMultipleFilesFromGallery()
	{
#if !UNITY_EDITOR && UNITY_ANDROID
		return AJC.CallStatic<bool>( "CanSelectMultipleMedia" );
#elif !UNITY_EDITOR && UNITY_IOS
		return _NativeGallery_CanPickMultipleMedia() == 1;
#else
		return false;
#endif
	}

	public static bool CanSelectMultipleMediaTypesFromGallery()
	{
#if UNITY_EDITOR
		return true;
#elif UNITY_ANDROID
		return AJC.CallStatic<bool>( "CanSelectMultipleMediaTypes" );
#elif UNITY_IOS
		return true;
#else
		return false;
#endif
	}

	public static Permission GetImageFromGallery( MediaPickCallback callback, string title = "", string mime = "image/*" )
	{
		return GetMediaFromGallery( callback, MediaType.Image, mime, title );
	}

	public static Permission GetVideoFromGallery( MediaPickCallback callback, string title = "", string mime = "video/*" )
	{
		return GetMediaFromGallery( callback, MediaType.Video, mime, title );
	}

	public static Permission GetAudioFromGallery( MediaPickCallback callback, string title = "", string mime = "audio/*" )
	{
		return GetMediaFromGallery( callback, MediaType.Audio, mime, title );
	}

	public static Permission GetMixedMediaFromGallery( MediaPickCallback callback, MediaType mediaTypes, string title = "" )
	{
		return GetMediaFromGallery( callback, mediaTypes, "*/*", title );
	}

	public static Permission GetImagesFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "image/*" )
	{
		return GetMultipleMediaFromGallery( callback, MediaType.Image, mime, title );
	}

	public static Permission GetVideosFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "video/*" )
	{
		return GetMultipleMediaFromGallery( callback, MediaType.Video, mime, title );
	}

	public static Permission GetAudiosFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "audio/*" )
	{
		return GetMultipleMediaFromGallery( callback, MediaType.Audio, mime, title );
	}

	public static Permission GetMixedMediasFromGallery( MediaPickMultipleCallback callback, MediaType mediaTypes, string title = "" )
	{
		return GetMultipleMediaFromGallery( callback, mediaTypes, "*/*", title );
	}

	public static bool IsMediaPickerBusy()
	{
#if !UNITY_EDITOR && UNITY_IOS
		return NGMediaReceiveCallbackiOS.IsBusy;
#else
		return false;
#endif
	}

	public static MediaType GetMediaTypeOfFile( string path )
	{
		if( string.IsNullOrEmpty( path ) )
			return (MediaType) 0;

		string extension = Path.GetExtension( path );
		if( string.IsNullOrEmpty( extension ) )
			return (MediaType) 0;

		if( extension[0] == '.' )
		{
			if( extension.Length == 1 )
				return (MediaType) 0;

			extension = extension.Substring( 1 );
		}

#if UNITY_EDITOR
		extension = extension.ToLowerInvariant();
		if( extension == "png" || extension == "jpg" || extension == "jpeg" || extension == "gif" || extension == "bmp" || extension == "tiff" )
			return MediaType.Image;
		else if( extension == "mp4" || extension == "mov" || extension == "wav" || extension == "avi" )
			return MediaType.Video;
		else if( extension == "mp3" || extension == "aac" || extension == "flac" )
			return MediaType.Audio;

		return (MediaType) 0;
#elif UNITY_ANDROID
		string mime = AJC.CallStatic<string>( "GetMimeTypeFromExtension", extension.ToLowerInvariant() );
		if( string.IsNullOrEmpty( mime ) )
			return (MediaType) 0;
		else if( mime.StartsWith( "image/" ) )
			return MediaType.Image;
		else if( mime.StartsWith( "video/" ) )
			return MediaType.Video;
		else if( mime.StartsWith( "audio/" ) )
			return MediaType.Audio;
		else
			return (MediaType) 0;
#elif UNITY_IOS
		return (MediaType) _NativeGallery_GetMediaTypeFromExtension( extension.ToLowerInvariant() );
#else
		return (MediaType) 0;
#endif
	}
	#endregion

	#region Internal Functions
	private static Permission SaveToGallery( byte[] mediaBytes, string album, string filename, MediaType mediaType, MediaSaveCallback callback )
	{
		Permission result = RequestPermission( PermissionType.Write );
		if( result == Permission.Granted )
		{
			if( mediaBytes == null || mediaBytes.Length == 0 )
				throw new ArgumentException( "Parameter 'mediaBytes' is null or empty!" );

			if( album == null || album.Length == 0 )
				throw new ArgumentException( "Parameter 'album' is null or empty!" );

			if( filename == null || filename.Length == 0 )
				throw new ArgumentException( "Parameter 'filename' is null or empty!" );

			if( string.IsNullOrEmpty( Path.GetExtension( filename ) ) )
				Debug.LogWarning( "'filename' doesn't have an extension, this might result in unexpected behaviour!" );

			string path = GetTemporarySavePath( filename );
#if UNITY_EDITOR
			Debug.Log( "SaveToGallery called successfully in the Editor" );
#else
			File.WriteAllBytes( path, mediaBytes );
#endif

			SaveToGalleryInternal( path, album, mediaType, callback );
		}

		return result;
	}

	private static Permission SaveToGallery( string existingMediaPath, string album, string filename, MediaType mediaType, MediaSaveCallback callback )
	{
		Permission result = RequestPermission( PermissionType.Write );
		if( result == Permission.Granted )
		{
			if( !File.Exists( existingMediaPath ) )
				throw new FileNotFoundException( "File not found at " + existingMediaPath );

			if( album == null || album.Length == 0 )
				throw new ArgumentException( "Parameter 'album' is null or empty!" );

			if( filename == null || filename.Length == 0 )
				throw new ArgumentException( "Parameter 'filename' is null or empty!" );

			if( string.IsNullOrEmpty( Path.GetExtension( filename ) ) )
			{
				string originalExtension = Path.GetExtension( existingMediaPath );
				if( string.IsNullOrEmpty( originalExtension ) )
					Debug.LogWarning( "'filename' doesn't have an extension, this might result in unexpected behaviour!" );
				else
					filename += originalExtension;
			}

			string path = GetTemporarySavePath( filename );
#if UNITY_EDITOR
			Debug.Log( "SaveToGallery called successfully in the Editor" );
#else
			File.Copy( existingMediaPath, path, true );
#endif

			SaveToGalleryInternal( path, album, mediaType, callback );
		}

		return result;
	}

	private static void SaveToGalleryInternal( string path, string album, MediaType mediaType, MediaSaveCallback callback )
	{
#if !UNITY_EDITOR && UNITY_ANDROID
		string savePath = AJC.CallStatic<string>( "SaveMedia", Context, (int) mediaType, path, album );

		File.Delete( path );

		if( callback != null )
			callback( !string.IsNullOrEmpty( savePath ), savePath );
#elif !UNITY_EDITOR && UNITY_IOS
		if( mediaType == MediaType.Audio )
		{
			Debug.LogError( "Saving audio files is not supported on iOS" );

			if( callback != null )
				callback( false, null );

			return;
		}

		Debug.Log( "Saving to Pictures: " + Path.GetFileName( path ) );

		NGMediaSaveCallbackiOS.Initialize( callback );
		if( mediaType == MediaType.Image )
			_NativeGallery_ImageWriteToAlbum( path, album, PermissionFreeMode ? 1 : 0 );
		else if( mediaType == MediaType.Video )
			_NativeGallery_VideoWriteToAlbum( path, album, PermissionFreeMode ? 1 : 0 );
#else
		if( callback != null )
			callback( true, null );
#endif
	}

	private static string GetTemporarySavePath( string filename )
	{
		string saveDir = Path.Combine( Application.persistentDataPath, "NGallery" );
		Directory.CreateDirectory( saveDir );

#if !UNITY_EDITOR && UNITY_IOS
		// Ensure a unique temporary filename on iOS:
		// iOS internally copies images/videos to Photos directory of the system,
		// but the process is async. The redundant file is deleted by objective-c code
		// automatically after the media is saved but while it is being saved, the file
		// should NOT be overwritten. Therefore, always ensure a unique filename on iOS
		string path = Path.Combine( saveDir, filename );
		if( File.Exists( path ) )
		{
			int fileIndex = 0;
			string filenameWithoutExtension = Path.GetFileNameWithoutExtension( filename );
			string extension = Path.GetExtension( filename );

			do
			{
				path = Path.Combine( saveDir, string.Concat( filenameWithoutExtension, ++fileIndex, extension ) );
			} while( File.Exists( path ) );
		}

		return path;
#else
		return Path.Combine( saveDir, filename );
#endif
	}

	private static Permission GetMediaFromGallery( MediaPickCallback callback, MediaType mediaType, string mime, string title )
	{
		Permission result = RequestPermission( PermissionType.Read );
		if( result == Permission.Granted && !IsMediaPickerBusy() )
		{
#if UNITY_EDITOR
			System.Collections.Generic.List<string> editorFilters = new System.Collections.Generic.List<string>( 4 );

			if( ( mediaType & MediaType.Image ) == MediaType.Image )
			{
				editorFilters.Add( "Image files" );
				editorFilters.Add( "png,jpg,jpeg" );
			}

			if( ( mediaType & MediaType.Video ) == MediaType.Video )
			{
				editorFilters.Add( "Video files" );
				editorFilters.Add( "mp4,mov,wav,avi" );
			}

			if( ( mediaType & MediaType.Audio ) == MediaType.Audio )
			{
				editorFilters.Add( "Audio files" );
				editorFilters.Add( "mp3,aac,flac" );
			}

			editorFilters.Add( "All files" );
			editorFilters.Add( "*" );

			string pickedFile = UnityEditor.EditorUtility.OpenFilePanelWithFilters( "Select file", "", editorFilters.ToArray() );

			if( callback != null )
				callback( pickedFile != "" ? pickedFile : null );
#elif UNITY_ANDROID
			AJC.CallStatic( "PickMedia", Context, new NGMediaReceiveCallbackAndroid( callback, null ), (int) mediaType, false, SelectedMediaPath, mime, title );
#elif UNITY_IOS
			if( mediaType == MediaType.Audio )
			{
				Debug.LogError( "Picking audio files is not supported on iOS" );

				if( callback != null ) // Selecting audio files is not supported on iOS
					callback( null );
			}
			else
			{
				NGMediaReceiveCallbackiOS.Initialize( callback, null );
				_NativeGallery_PickMedia( SelectedMediaPath, (int) ( mediaType & ~MediaType.Audio ), PermissionFreeMode ? 1 : 0, 1 );
			}
#else
			if( callback != null )
				callback( null );
#endif
		}

		return result;
	}

	private static Permission GetMultipleMediaFromGallery( MediaPickMultipleCallback callback, MediaType mediaType, string mime, string title )
	{
		Permission result = RequestPermission( PermissionType.Read );
		if( result == Permission.Granted && !IsMediaPickerBusy() )
		{
			if( CanSelectMultipleFilesFromGallery() )
			{
#if !UNITY_EDITOR && UNITY_ANDROID
				AJC.CallStatic( "PickMedia", Context, new NGMediaReceiveCallbackAndroid( null, callback ), (int) mediaType, true, SelectedMediaPath, mime, title );
#elif !UNITY_EDITOR && UNITY_IOS
				if( mediaType == MediaType.Audio )
				{
					Debug.LogError( "Picking audio files is not supported on iOS" );

					if( callback != null ) // Selecting audio files is not supported on iOS
						callback( null );
				}
				else
				{
					NGMediaReceiveCallbackiOS.Initialize( null, callback );
					_NativeGallery_PickMedia( SelectedMediaPath, (int) ( mediaType & ~MediaType.Audio ), PermissionFreeMode ? 1 : 0, 0 );
				}
#else
				if( callback != null )
					callback( null );
#endif
			}
			else if( callback != null )
				callback( null );
		}

		return result;
	}

	private static byte[] GetTextureBytes( Texture2D texture, bool isJpeg )
	{
		try
		{
			return isJpeg ? texture.EncodeToJPG( 100 ) : texture.EncodeToPNG();
		}
		catch( UnityException )
		{
			return GetTextureBytesFromCopy( texture, isJpeg );
		}
		catch( ArgumentException )
		{
			return GetTextureBytesFromCopy( texture, isJpeg );
		}

#pragma warning disable 0162
		return null;
#pragma warning restore 0162
	}

	private static byte[] GetTextureBytesFromCopy( Texture2D texture, bool isJpeg )
	{
		// Texture is marked as non-readable, create a readable copy and save it instead
		Debug.LogWarning( "Saving non-readable textures is slower than saving readable textures" );

		Texture2D sourceTexReadable = null;
		RenderTexture rt = RenderTexture.GetTemporary( texture.width, texture.height );
		RenderTexture activeRT = RenderTexture.active;

		try
		{
			Graphics.Blit( texture, rt );
			RenderTexture.active = rt;

			sourceTexReadable = new Texture2D( texture.width, texture.height, isJpeg ? TextureFormat.RGB24 : TextureFormat.RGBA32, false );
			sourceTexReadable.ReadPixels( new Rect( 0, 0, texture.width, texture.height ), 0, 0, false );
			sourceTexReadable.Apply( false, false );
		}
		catch( Exception e )
		{
			Debug.LogException( e );

			Object.DestroyImmediate( sourceTexReadable );
			return null;
		}
		finally
		{
			RenderTexture.active = activeRT;
			RenderTexture.ReleaseTemporary( rt );
		}

		try
		{
			return isJpeg ? sourceTexReadable.EncodeToJPG( 100 ) : sourceTexReadable.EncodeToPNG();
		}
		catch( Exception e )
		{
			Debug.LogException( e );
			return null;
		}
		finally
		{
			Object.DestroyImmediate( sourceTexReadable );
		}
	}
	#endregion

	#region Utility Functions
	public static Texture2D LoadImageAtPath( string imagePath, int maxSize = -1, bool markTextureNonReadable = true,
		bool generateMipmaps = true, bool linearColorSpace = false )
	{
		if( string.IsNullOrEmpty( imagePath ) )
			throw new ArgumentException( "Parameter 'imagePath' is null or empty!" );

		if( !File.Exists( imagePath ) )
			throw new FileNotFoundException( "File not found at " + imagePath );

		if( maxSize <= 0 )
			maxSize = SystemInfo.maxTextureSize;

#if !UNITY_EDITOR && UNITY_ANDROID
		string loadPath = AJC.CallStatic<string>( "LoadImageAtPath", Context, imagePath, TemporaryImagePath, maxSize );
#elif !UNITY_EDITOR && UNITY_IOS
		string loadPath = _NativeGallery_LoadImageAtPath( imagePath, TemporaryImagePath, maxSize );
#else
		string loadPath = imagePath;
#endif

		string extension = Path.GetExtension( imagePath ).ToLowerInvariant();
		TextureFormat format = ( extension == ".jpg" || extension == ".jpeg" ) ? TextureFormat.RGB24 : TextureFormat.RGBA32;

		Texture2D result = new Texture2D( 2, 2, format, generateMipmaps, linearColorSpace );

		try
		{
			if( !result.LoadImage( File.ReadAllBytes( loadPath ), markTextureNonReadable ) )
			{
				Object.DestroyImmediate( result );
				return null;
			}
		}
		catch( Exception e )
		{
			Debug.LogException( e );

			Object.DestroyImmediate( result );
			return null;
		}
		finally
		{
			if( loadPath != imagePath )
			{
				try
				{
					File.Delete( loadPath );
				}
				catch { }
			}
		}

		return result;
	}

	public static Texture2D GetVideoThumbnail( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true )
	{
		if( maxSize <= 0 )
			maxSize = SystemInfo.maxTextureSize;

#if !UNITY_EDITOR && UNITY_ANDROID
		string thumbnailPath = AJC.CallStatic<string>( "GetVideoThumbnail", Context, videoPath, TemporaryImagePath + ".png", false, maxSize, captureTimeInSeconds );
#elif !UNITY_EDITOR && UNITY_IOS
		string thumbnailPath = _NativeGallery_GetVideoThumbnail( videoPath, TemporaryImagePath + ".png", maxSize, captureTimeInSeconds );
#else
		string thumbnailPath = null;
#endif

		if( !string.IsNullOrEmpty( thumbnailPath ) )
			return LoadImageAtPath( thumbnailPath, maxSize, markTextureNonReadable );
		else
			return null;
	}

	public static ImageProperties GetImageProperties( string imagePath )
	{
		if( !File.Exists( imagePath ) )
			throw new FileNotFoundException( "File not found at " + imagePath );

#if !UNITY_EDITOR && UNITY_ANDROID
		string value = AJC.CallStatic<string>( "GetImageProperties", Context, imagePath );
#elif !UNITY_EDITOR && UNITY_IOS
		string value = _NativeGallery_GetImageProperties( imagePath );
#else
		string value = null;
#endif

		int width = 0, height = 0;
		string mimeType = null;
		ImageOrientation orientation = ImageOrientation.Unknown;
		if( !string.IsNullOrEmpty( value ) )
		{
			string[] properties = value.Split( '>' );
			if( properties != null && properties.Length >= 4 )
			{
				if( !int.TryParse( properties[0].Trim(), out width ) )
					width = 0;
				if( !int.TryParse( properties[1].Trim(), out height ) )
					height = 0;

				mimeType = properties[2].Trim();
				if( mimeType.Length == 0 )
				{
					string extension = Path.GetExtension( imagePath ).ToLowerInvariant();
					if( extension == ".png" )
						mimeType = "image/png";
					else if( extension == ".jpg" || extension == ".jpeg" )
						mimeType = "image/jpeg";
					else if( extension == ".gif" )
						mimeType = "image/gif";
					else if( extension == ".bmp" )
						mimeType = "image/bmp";
					else
						mimeType = null;
				}

				int orientationInt;
				if( int.TryParse( properties[3].Trim(), out orientationInt ) )
					orientation = (ImageOrientation) orientationInt;
			}
		}

		return new ImageProperties( width, height, mimeType, orientation );
	}

	public static VideoProperties GetVideoProperties( string videoPath )
	{
		if( !File.Exists( videoPath ) )
			throw new FileNotFoundException( "File not found at " + videoPath );

#if !UNITY_EDITOR && UNITY_ANDROID
		string value = AJC.CallStatic<string>( "GetVideoProperties", Context, videoPath );
#elif !UNITY_EDITOR && UNITY_IOS
		string value = _NativeGallery_GetVideoProperties( videoPath );
#else
		string value = null;
#endif

		int width = 0, height = 0;
		long duration = 0L;
		float rotation = 0f;
		if( !string.IsNullOrEmpty( value ) )
		{
			string[] properties = value.Split( '>' );
			if( properties != null && properties.Length >= 4 )
			{
				if( !int.TryParse( properties[0].Trim(), out width ) )
					width = 0;
				if( !int.TryParse( properties[1].Trim(), out height ) )
					height = 0;
				if( !long.TryParse( properties[2].Trim(), out duration ) )
					duration = 0L;
				if( !float.TryParse( properties[3].Trim(), out rotation ) )
					rotation = 0f;
			}
		}

		if( rotation == -90f )
			rotation = 270f;

		return new VideoProperties( width, height, duration, rotation );
	}
	#endregion
}