# UnityNativeGallery_HarmonyOS **Repository Path**: chenhao8721/unity-native-gallery-HarmonyOS ## Basic Information - **Project Name**: UnityNativeGallery_HarmonyOS - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-04-18 - **Last Updated**: 2024-04-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Available on Asset Store: https://assetstore.unity.com/packages/tools/integration/native-gallery-for-android-ios-112630 Forum Thread: https://forum.unity.com/threads/native-gallery-for-android-ios-open-source.519619/ Discord: https://discord.gg/UJJt549AaV GitHub Sponsors ☕ 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. INSTALLATION There are 5 ways to install this plugin: import NativeGallery.unitypackage via Assets-Import Package clone/download this repository and move the Plugins folder to your Unity project's Assets folder import it from Asset Store (via Package Manager) add the following line to Packages/manifest.json: "com.yasirkula.nativegallery": "https://github.com/yasirkula/UnityNativeGallery.git", (via OpenUPM) after installing openupm-cli, run the following command: openupm add com.yasirkula.nativegallery Android Setup NativeGallery no longer requires any manual setup on Android. iOS Setup IMPORTANT: If you are targeting iOS 14 or later, you need to build your app with Xcode 12 or later to avoid any permission issues. There are two ways to set up the plugin on iOS: a. Automated Setup for iOS (optional) change the values of Photo Library Usage Description and Photo Library Additions Usage Description at Project Settings/yasirkula/Native Gallery (Unity 2017.4 or earlier) if your minimum Deployment Target (iOS Version) is at least 8.0, set the value of Deployment Target Is 8.0 Or Above to true at Project Settings/yasirkula/Native Gallery b. Manual Setup for iOS see: https://github.com/yasirkula/UnityNativeGallery/wiki/Manual-Setup-for-iOS FAQ How can I fetch the path of the saved image or the original path of the picked image on iOS? You can't. On iOS, these files are stored in an internal directory that we have no access to (I don't think there is even a way to fetch that internal path). Can't access the Gallery, it says "java.lang.ClassNotFoundException: com.yasirkula.unity.NativeGallery" in Logcat If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.* { *; } Android build fails, it says "error: attribute android:requestLegacyExternalStorage not found" in Console android:requestLegacyExternalStorage attribute in AndroidManifest.xml fixes a rare UnauthorizedAccessException on Android 10 but requires you to update your Android SDK to at least SDK 29. If this isn't possible for you, you should open NativeGallery.aar with WinRAR or 7-Zip and then remove the tag from AndroidManifest.xml. Nothing happens when I try to access the Gallery on Android Make sure that you've set the Write Permission to External (SDCard) in Player Settings. NativeGallery functions return Permission.Denied even though I've set "Write Permission" to "External (SDCard)" Declare the permission manually in your Plugins/Android/AndroidManifest.xml file with the attribute as follows: (you'll need to add the attribute to the element).WRITE_EXTERNAL_STORAGEtools:node="replace"xmlns:tools="http://schemas.android.com/tools" Saving image/video doesn't work properly Make sure that the filename parameter of the Save function includes the file's extension, as well HOW TO A. Saving Media To Gallery/Photos NativeGallery.SaveImageToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null ): use this function if you have the raw bytes of the image. On Android, your images/videos are saved at DCIM/album/filename. On iOS 14+, the image/video will be saved to the default Photos album (i.e. album parameter will be ignored). On earlier iOS versions, the image/video will be saved to the target album. Make sure that the filename parameter includes the file's extension, as well MediaSaveCallback takes and parameters. If the image/video is saved successfully, success becomes true. On Android, path stores where the image/video was saved to (is null on iOS). If the raw filepath can't be determined, an abstract Storage Access Framework path will be returned (File.Exists returns false for that path)bool successstring path IMPORTANT: NativeGallery will never overwrite existing media on the Gallery. If there is a name conflict, NativeGallery will ensure a unique filename. So don't put in filename anymore (for new users, putting {0} in filename was recommended in order to ensure unique filenames in earlier versions, this is no longer necessary).{0} NativeGallery.SaveImageToGallery( string existingMediaPath, string album, string filename, MediaSaveCallback callback = null ): use this function if the image is already saved on disk. Enter the file's path to existingMediaPath. NativeGallery.SaveImageToGallery( Texture2D image, string album, string filename, MediaSaveCallback callback = null ): use this function to easily save a Texture2D to Gallery/Photos. If filename ends with ".jpeg" or ".jpg", texture will be saved as JPEG; otherwise, it will be saved as PNG. NativeGallery.SaveVideoToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null ): use this function if you have the raw bytes of the video. This function works similar to its SaveImageToGallery equivalent. NativeGallery.SaveVideoToGallery( string existingMediaPath, string album, string filename, MediaSaveCallback callback = null ): use this function if the video is already saved on disk. This function works similar to its SaveImageToGallery equivalent. B. Retrieving Media From Gallery/Photos NativeGallery.GetImageFromGallery( MediaPickCallback callback, string title = "", string mime = "image/*" ): prompts the user to select an image from Gallery/Photos. This operation is asynchronous! After user selects an image or cancels the operation, the callback is called (on main thread). MediaPickCallback takes a string parameter which stores the path of the selected image, or null if nothing is selected title determines the title of the image picker dialog on Android. Has no effect on iOS mime filters the available images on Android. For example, to request a JPEG image from the user, mime can be set as "image/jpeg". Setting multiple mime types is not possible (in that case, you should leave mime as "image/*"). Has no effect on iOS NativeGallery.GetVideoFromGallery( MediaPickCallback callback, string title = "", string mime = "video/*" ): prompts the user to select a video from Gallery/Photos. This function works similar to its GetImageFromGallery equivalent. NativeGallery.GetAudioFromGallery( MediaPickCallback callback, string title = "", string mime = "audio/*" ): prompts the user to select an audio file. This function works similar to its GetImageFromGallery equivalent. Works on Android only. NativeGallery.GetMixedMediaFromGallery( MediaPickCallback callback, MediaType mediaTypes, string title = "" ): prompts the user to select an image/video/audio file. This function is available on Android 19 and later and all iOS versions. Selecting audio files is not supported on iOS. mediaTypes is the bitwise OR'ed media types that will be displayed in the file picker dialog (e.g. to pick an image or video, use MediaType.Image | MediaType.Video) NativeGallery.GetImagesFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "image/*" ): prompts the user to select one or more images from Gallery/Photos. MediaPickMultipleCallback takes a string[] parameter which stores the path(s) of the selected image(s)/video(s), or null if nothing is selected. Selecting multiple files from gallery is only available on Android 18 and later and iOS 14 and later. Call CanSelectMultipleFilesFromGallery() to see if this feature is available. NativeGallery.GetVideosFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "video/*" ): prompts the user to select one or more videos from Gallery/Photos. This function works similar to its GetImagesFromGallery equivalent. NativeGallery.GetAudiosFromGallery( MediaPickMultipleCallback callback, string title = "", string mime = "audio/*" ): prompts the user to select one or more audio files. This function works similar to its GetImagesFromGallery equivalent. Works on Android only. NativeGallery.GetMixedMediasFromGallery( MediaPickMultipleCallback callback, MediaType mediaTypes, string title = "" ): prompts the user to select one or more image/video/audio files. Selecting audio files is not supported on iOS. NativeGallery.CanSelectMultipleFilesFromGallery(): returns true if selecting multiple images/videos from Gallery/Photos is possible on this device. NativeGallery.CanSelectMultipleMediaTypesFromGallery(): returns true if GetMixedMediaFromGallery/GetMixedMediasFromGallery functions are supported on this device. NativeGallery.IsMediaPickerBusy(): returns true if the user is currently picking media from Gallery/Photos. In that case, another GetImageFromGallery, GetVideoFromGallery or GetAudioFromGallery request will simply be ignored. Almost all of these functions return a NativeGallery.Permission value. More details about it is available below. C. Runtime Permissions Beginning with 6.0 Marshmallow, Android apps must request runtime permissions before accessing certain services, similar to iOS. Note that NativeGallery doesn't require any permissions for picking images/videos from Photos on iOS 11+, picking images/videos from Gallery on Android 34+ and saving images/videos to Gallery on Android 29+, so no permission dialog will be shown in these cases and the permission functions will return Permission.Granted. There are two functions to handle permissions with this plugin: NativeGallery.Permission NativeGallery.CheckPermission( PermissionType permissionType, MediaType mediaTypes ): checks whether the app has access to Gallery/Photos or not. PermissionType can be either Read (for GetImageFromGallery/GetVideoFromGallery functions) or Write (for SaveImageToGallery/SaveVideoToGallery functions). mediaTypes determines for which media type(s) we're checking the permission for. Has no effect on iOS NativeGallery.Permission is an enum that can take 3 values: Granted: we have the permission to access Gallery/Photos ShouldAsk: we don't have permission yet, but we can ask the user for permission via RequestPermission function (see below). On Android, as long as the user doesn't select "Don't ask again" while denying the permission, ShouldAsk is returned Denied: we don't have permission and we can't ask the user for permission. In this case, user has to give the permission from Settings. This happens when user denies the permission on iOS (can't request permission again on iOS), when user selects "Don't ask again" while denying the permission on Android or when user is not allowed to give that permission (parental controls etc.) NativeGallery.Permission NativeGallery.RequestPermission( PermissionType permissionType, MediaType mediaTypes ): requests permission to access Gallery/Photos from the user and returns the result. It is recommended to show a brief explanation before asking the permission so that user understands why the permission is needed and doesn't click Deny or worse, "Don't ask again". Note that the SaveImageToGallery/SaveVideoToGallery and GetImageFromGallery/GetVideoFromGallery functions call RequestPermission internally and execute only if the permission is granted (the result of RequestPermission is also returned). void NativeGallery.RequestPermissionAsync( PermissionCallback callback, PermissionType permissionType, MediaType mediaTypes ): Asynchronous variant of RequestPermission. Unlike RequestPermission, this function doesn't freeze the app unnecessarily before the permission dialog is displayed. So it's recommended to call this function instead. PermissionCallback takes parameterNativeGallery.Permission permission Task NativeGallery.RequestPermissionAsync( PermissionType permissionType, MediaType mediaTypes ): Another asynchronous variant of RequestPermission (requires Unity 2018.4 or later). NativeGallery.OpenSettings(): opens the settings for this app, from where the user can manually grant permission in case current permission state is Permission.Denied (on Android, the necessary permission is named Storage and on iOS, the necessary permission is named Photos). bool NativeGallery.CanOpenSettings(): on iOS versions prior to 8.0, opening settings from within app is not possible and in this case, this function returns false. Otherwise, it returns true. D. Utility Functions NativeGallery.ImageProperties NativeGallery.GetImageProperties( string imagePath ): returns an ImageProperties instance that holds the width, height, mime type and EXIF orientation information of an image file without creating a Texture2D object. Mime type will be null, if it can't be determined 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 generateMipmaps determines whether texture should have mipmaps or not linearColorSpace determines whether texture should be in linear color space or sRGB color space async Task NativeGallery.LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true ): asynchronous variant of LoadImageAtPath (requires Unity 2018.4 or later). Whether or not the returned Texture2D has mipmaps enabled depends on UnityWebRequestTexture's implementation on the target Unity version. Note that it isn't possible to load multiple images simultaneously using this function. Texture2D NativeGallery.GetVideoThumbnail( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false ): creates a Texture2D thumbnail from a video file and returns it. Returns null, if something goes wrong. maxSize determines the maximum size of the returned Texture2D in pixels. Larger thumbnails 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 captureTimeInSeconds determines the frame of the video that the thumbnail is captured from. If untouched, OS will decide this value markTextureNonReadable (see LoadImageAtPath) async Task NativeGallery.GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true ): asynchronous variant of GetVideoThumbnail (requires Unity 2018.4 or later). Whether or not the returned Texture2D has mipmaps enabled depends on UnityWebRequestTexture's implementation on the target Unity version. Note that it isn't possible to generate multiple video thumbnails simultaneously using this function.