Available on Asset Store: https://www.assetstore.unity3d.com/en/#!/content/112630
Forum Thread: https://forum.unity.com/threads/native-gallery-for-android-ios-open-source.519619/
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. It takes only a couple of steps to set everything up:
After importing NativeGallery.unitypackage to your project, only a few steps are required to set up the plugin:
Set Write Permission to External (SDCard) in Player Settings. Alternatively, if your app won't be saving media to the Gallery but instead just reading media from it, you can add READ_EXTERNAL_STORAGE
permission to your AndroidManifest.
There are two ways to set up the plugin on iOS:
a. Automated Setup for iOS
b. Manual Setup for iOS
If your project uses ProGuard, try adding the following line to ProGuard filters: -keep class com.yasirkula.unity.* { *; }
Make sure that you've set the Write Permission to External (SDCard) in Player Settings.
Make sure that the filename parameter of the Save function includes the file's extension, as well
NativeGallery.SaveImageToGallery( byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null )
: use this function if you have the raw bytes of the image.
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 {0}
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).
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.
NativeGallery.GetImageFromGallery( MediaPickCallback callback, string title = "", string mime = "image/*" )
: prompts the user to select an image from Gallery/Photos.
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.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 (iOS not supported). 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.CanSelectMultipleFilesFromGallery()
: returns true if selecting multiple images/videos from Gallery/Photos is possible on this device.
NativeGallery.IsMediaPickerBusy()
: returns true if the user is currently picking media from Gallery/Photos. In that case, another GetImageFromGallery or GetVideoFromGallery request will simply be ignored.
Almost all of these functions return a NativeGallery.Permission value. More details about it is available below.
Beginning with 6.0 Marshmallow, Android apps must request runtime permissions before accessing certain services, similar to iOS. There are two functions to handle permissions with this plugin:
NativeGallery.Permission NativeGallery.CheckPermission()
: checks whether the app has access to Gallery/Photos or not.
NativeGallery.Permission is an enum that can take 3 values:
NativeGallery.Permission NativeGallery.RequestPermission()
: 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).
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.
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.
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.
The following code has three functions:
void Update()
{
if( Input.GetMouseButtonDown( 0 ) )
{
if( Input.mousePosition.x < Screen.width / 3 )
{
// Take a screenshot and save it to Gallery/Photos
StartCoroutine( TakeScreenshotAndSave() );
}
else
{
// Don't attempt to pick media from Gallery/Photos if
// another media pick operation is already in progress
if( NativeGallery.IsMediaPickerBusy() )
return;
if( Input.mousePosition.x < Screen.width * 2 / 3 )
{
// Pick a PNG image from Gallery/Photos
// If the selected image's width and/or height is greater than 512px, down-scale the image
PickImage( 512 );
}
else
{
// Pick a video from Gallery/Photos
PickVideo();
}
}
}
}
private IEnumerator TakeScreenshotAndSave()
{
yield return new WaitForEndOfFrame();
Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
ss.Apply();
// Save the screenshot to Gallery/Photos
Debug.Log( "Permission result: " + NativeGallery.SaveImageToGallery( ss, "GalleryTest", "Image.png" ) );
// To avoid memory leaks
Destroy( ss );
}
private void PickImage( int maxSize )
{
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery( ( path ) =>
{
Debug.Log( "Image path: " + path );
if( path != null )
{
// Create Texture from selected image
Texture2D texture = NativeGallery.LoadImageAtPath( path, maxSize );
if( texture == null )
{
Debug.Log( "Couldn't load texture from " + path );
return;
}
// Assign texture to a temporary quad and destroy it after 5 seconds
GameObject quad = GameObject.CreatePrimitive( PrimitiveType.Quad );
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
quad.transform.forward = Camera.main.transform.forward;
quad.transform.localScale = new Vector3( 1f, texture.height / (float) texture.width, 1f );
Material material = quad.GetComponent<Renderer>().material;
if( !material.shader.isSupported ) // happens when Standard shader is not included in the build
material.shader = Shader.Find( "Legacy Shaders/Diffuse" );
material.mainTexture = texture;
Destroy( quad, 5f );
// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
Destroy( texture, 5f );
}
}, "Select a PNG image", "image/png" );
Debug.Log( "Permission result: " + permission );
}
private void PickVideo()
{
NativeGallery.Permission permission = NativeGallery.GetVideoFromGallery( ( path ) =>
{
Debug.Log( "Video path: " + path );
if( path != null )
{
// Play the selected video
Handheld.PlayFullScreenMovie( "file://" + path );
}
}, "Select a video" );
Debug.Log( "Permission result: " + permission );
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。