Skip to content

Commit

Permalink
Added GetMediaTypeOfFile function (closed #133)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Aug 12, 2020
1 parent 5584831 commit 93f9b95
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ public static boolean CanSelectMultipleMediaTypes()
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}

public static String GetMimeTypeFromExtension( String extension )
{
if( extension == null || extension.length() == 0 )
return "";

String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension( extension.toLowerCase( Locale.ENGLISH ) );
return mime != null ? mime : "";
}

public static String LoadImageAtPath( Context context, String path, final String temporaryFilePath, final int maxSize )
{
return NativeGalleryUtils.LoadImageAtPath( context, path, temporaryFilePath, maxSize );
Expand Down
2 changes: 2 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Beginning with *6.0 Marshmallow*, Android apps must request runtime permissions

`NativeGallery.VideoProperties NativeGallery.GetVideoProperties( string videoPath )`: returns a *VideoProperties* instance that holds the width, height, duration (in milliseconds) and rotation information of a video file. To play a video in correct orientation, you should rotate it by *rotation* degrees clockwise. For a 90-degree or 270-degree rotated video, values of *width* and *height* should be swapped to get the display size of the video.

`NativeGallery.MediaType NativeGallery.GetMediaTypeOfFile( string path )`: returns the media type of the file at the specified path: *Image*, *Video*, *Audio* or neither of these (if media type can't be determined)

`Texture2D NativeGallery.LoadImageAtPath( string imagePath, int maxSize = -1, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )`: creates a Texture2D from the specified image file in correct orientation and returns it. Returns *null*, if something goes wrong.
- **maxSize** determines the maximum size of the returned Texture2D in pixels. Larger textures will be down-scaled. If untouched, its value will be set to *SystemInfo.maxTextureSize*. It is recommended to set a proper maxSize for better performance
- **markTextureNonReadable** marks the generated texture as non-readable for better memory usage. If you plan to modify the texture later (e.g. *GetPixels*/*SetPixels*), set its value to *false*
Expand Down
Binary file modified Plugins/NativeGallery/Android/NativeGallery.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions Plugins/NativeGallery/Android/NativeGallery.jar.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Plugins/NativeGallery/NativeGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ private static AndroidJavaObject Context
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void _NativeGallery_OpenSettings();

[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 );

Expand Down Expand Up @@ -325,6 +328,42 @@ public static bool IsMediaPickerBusy()
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 && 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_EDITOR && UNITY_IOS
return (MediaType) _NativeGallery_GetMediaTypeFromExtension( extension.ToLowerInvariant() );
#else
return (MediaType) 0;
#endif
}
#endregion

#region Internal Functions
Expand Down
5 changes: 4 additions & 1 deletion Plugins/NativeGallery/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ Texture2D NativeGallery.GetVideoThumbnail( string videoPath, int maxSize = -1, d
NativeGallery.ImageProperties NativeGallery.GetImageProperties( string imagePath );

// Returns a VideoProperties instance that holds the width, height, duration (in milliseconds) and rotation information of a video file. To play a video in correct orientation, you should rotate it by rotation degrees clockwise. For a 90-degree or 270-degree rotated video, values of width and height should be swapped to get the display size of the video
NativeGallery.VideoProperties NativeGallery.GetVideoProperties( string videoPath );
NativeGallery.VideoProperties NativeGallery.GetVideoProperties( string videoPath );

// Returns the media type of the file at the specified path: Image, Video, Audio or neither of these (if media type can't be determined)
NativeGallery.MediaType NativeGallery.GetMediaTypeOfFile( string path );
27 changes: 27 additions & 0 deletions Plugins/NativeGallery/iOS/NativeGallery.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/ImageIO.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
#import <AssetsLibrary/AssetsLibrary.h>
Expand All @@ -20,6 +21,7 @@ + (void)openSettings;
+ (void)saveMedia:(NSString *)path albumName:(NSString *)album isImg:(BOOL)isImg;
+ (void)pickMedia:(int)mediaType savePath:(NSString *)mediaSavePath;
+ (int)isMediaPickerBusy;
+ (int)getMediaTypeFromExtension:(NSString *)extension;
+ (char *)getImageProperties:(NSString *)path;
+ (char *)getVideoProperties:(NSString *)path;
+ (char *)getVideoThumbnail:(NSString *)path savePath:(NSString *)savePath maximumSize:(int)maximumSize captureTime:(double)captureTime;
Expand Down Expand Up @@ -357,6 +359,27 @@ + (int)isMediaPickerBusy {
return 0;
}

// Credit: https://lists.apple.com/archives/cocoa-dev/2012/Jan/msg00052.html
+ (int)getMediaTypeFromExtension:(NSString *)extension {
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef) extension, NULL);

// mediaType is a bitmask:
// 1: image
// 2: video
// 4: audio (not supported)
int result = 0;
if (UTTypeConformsTo(fileUTI, kUTTypeImage))
result = 1;
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie) || UTTypeConformsTo(fileUTI, kUTTypeVideo))
result = 2;
else if (UTTypeConformsTo(fileUTI, kUTTypeAudio))
result = 4;

CFRelease(fileUTI);

return result;
}

// Credit: https://stackoverflow.com/a/4170099/2373034
+ (NSArray *)getImageMetadata:(NSString *)path {
int width = 0;
Expand Down Expand Up @@ -769,6 +792,10 @@ + (char *)getCString:(NSString *)source {
return [UNativeGallery isMediaPickerBusy];
}

extern "C" int _NativeGallery_GetMediaTypeFromExtension(const char* extension) {
return [UNativeGallery getMediaTypeFromExtension:[NSString stringWithUTF8String:extension]];
}

extern "C" char* _NativeGallery_GetImageProperties(const char* path) {
return [UNativeGallery getImageProperties:[NSString stringWithUTF8String:path]];
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.yasirkula.nativegallery",
"displayName": "Native Gallery",
"version": "1.5.0",
"version": "1.5.1",
"description": "This plugin helps you save your images and/or videos to device Gallery on Android and Photos on iOS. It is also possible to pick an image or video from Gallery/Photos."
}

0 comments on commit 93f9b95

Please sign in to comment.