Пример #1
0
 public static Permission SaveVideoToGallery(string existingMediaPath, string album, string filenameFormatted, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(existingMediaPath, album, filenameFormatted, false, callback));
 }
Пример #2
0
    private static Permission SaveToGallery(byte[] mediaBytes, string album, string filenameFormatted, bool isImage, MediaSaveCallback callback)
    {
        Permission result = RequestPermission();

        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 (filenameFormatted == null || filenameFormatted.Length == 0)
            {
                throw new ArgumentException("Parameter 'filenameFormatted' is null or empty!");
            }

            string path = GetSavePath(album, filenameFormatted);

            File.WriteAllBytes(path, mediaBytes);

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

        return(result);
    }
Пример #3
0
    public static Permission SaveImageToGallery(Texture2D image, string album, string filenameFormatted, MediaSaveCallback callback = null)
    {
        if (image == null)
        {
            throw new ArgumentException("Parameter 'image' is null!");
        }

        if (filenameFormatted.EndsWith(".jpeg") || filenameFormatted.EndsWith(".jpg"))
        {
            return(SaveToGallery(image.EncodeToJPG(100), album, filenameFormatted, true, callback));
        }
        else if (filenameFormatted.EndsWith(".png"))
        {
            return(SaveToGallery(image.EncodeToPNG(), album, filenameFormatted, true, callback));
        }
        else
        {
            return(SaveToGallery(image.EncodeToPNG(), album, filenameFormatted + ".png", true, callback));
        }
    }
Пример #4
0
 public static Permission SaveVideoToGallery(byte[] mediaBytes, string album, string filenameFormatted, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(mediaBytes, album, filenameFormatted, false, callback));
 }
Пример #5
0
    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);
    }
Пример #6
0
    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
    }
Пример #7
0
 private static Permission SaveAudioToGallery(string existingMediaPath, string album, string filename, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(existingMediaPath, album, filename, MediaType.Audio, callback));
 }
Пример #8
0
    private static Permission SaveToGallery(string existingMediaPath, string album, string filename, MediaType mediaType, MediaSaveCallback callback)
    {
        Permission result = RequestPermission(false);

        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!");
            }

            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);
    }
Пример #9
0
    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));
        }
    }
Пример #10
0
 private static Permission SaveAudioToGallery(byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(mediaBytes, album, filename, MediaType.Audio, callback));
 }
Пример #11
0
 public static Permission SaveImageToGallery(string existingMediaPath, string album, string filename, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(existingMediaPath, album, filename, MediaType.Image, callback));
 }
Пример #12
0
 public static Permission SaveImageToGallery(byte[] mediaBytes, string album, string filename, MediaSaveCallback callback = null)
 {
     return(SaveToGallery(mediaBytes, album, filename, MediaType.Image, callback));
 }
Пример #13
0
    private static void SaveToGalleryInternal(string path, string album, MediaType mediaType, MediaSaveCallback callback)
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        AJC.CallStatic("SaveMedia", Context, (int)mediaType, path, album);

        File.Delete(path);

        if (callback != null)
        {
            callback(null);
        }
#elif !UNITY_EDITOR && UNITY_IOS
        if (mediaType == MediaType.Audio)
        {
            if (callback != null)
            {
                callback("Saving audio files is not supported on iOS");
            }

            return;
        }

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

        NGMediaSaveCallbackiOS.Initialize(callback);
        if (mediaType == MediaType.Image)
        {
            _NativeGallery_ImageWriteToAlbum(path, album);
        }
        else if (mediaType == MediaType.Video)
        {
            _NativeGallery_VideoWriteToAlbum(path, album);
        }
#else
        if (callback != null)
        {
            callback(null);
        }
#endif
    }
Пример #14
0
    private static Permission SaveToGallery(string existingMediaPath, string album, string filenameFormatted, bool isImage, MediaSaveCallback callback)
    {
        Permission result = RequestPermission();

        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 (filenameFormatted == null || filenameFormatted.Length == 0)
            {
                throw new ArgumentException("Parameter 'filenameFormatted' is null or empty!");
            }

            string path = GetSavePath(album, filenameFormatted);

            File.Copy(existingMediaPath, path, true);

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

        return(result);
    }
Пример #15
0
    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);
    }
Пример #16
0
    private static void SaveToGalleryInternal(string path, string album, bool isImage, MediaSaveCallback callback)
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        AJC.CallStatic("MediaScanFile", Context, path);

        if (callback != null)
        {
            callback(null);
        }

        Debug.Log("Saving to gallery: " + path);
#elif !UNITY_EDITOR && UNITY_IOS
        NGMediaSaveCallbackiOS.Initialize(callback);
        if (isImage)
        {
            _NativeGallery_ImageWriteToAlbum(path, album);
        }
        else
        {
            _NativeGallery_VideoWriteToAlbum(path, album);
        }

        Debug.Log("Saving to Pictures: " + Path.GetFileName(path));
#else
        if (callback != null)
        {
            callback(null);
        }
#endif
    }
Пример #17
0
    private static Permission SaveToGallery(byte[] mediaBytes, string album, string filename, MediaType mediaType, MediaSaveCallback callback)
    {
        Permission result = RequestPermission(false);

        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!");
            }

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