Search Unity

Native Gallery for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Feb 28, 2018.

  1. Lordmin

    Lordmin

    Joined:
    Mar 9, 2017
    Posts:
    62
    Thanks for the amazing asset!

    I tried using test code.

    Can I only open certain folders on Android/iOS, not the entire gallery?

    Exactly what you want is to open only certain folders on both platforms and select videos to play with the video players supported by each platform.
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeGallery doesn't have this feature, sorry :/
     
  3. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    Hey there I changed the ios lines as you recommended, I'm not sure there's much difference?

    maxsize=50


    -maxsize= 512 +
    TextureScale.Bilinear(Wall.instance.textureFromPath, 50, 50);


    Maybe a bit better? not sure
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I agree, there is almost no difference. I'm not planning to improve the resize algorithm any further, though. You can try TextureScale.Point since you are looking for sharp output.
     
  5. Suwas93

    Suwas93

    Joined:
    Feb 8, 2018
    Posts:
    62
    It is indeed failing after 32. Adding Time stamp and checking
     
  6. Suwas93

    Suwas93

    Joined:
    Feb 8, 2018
    Posts:
    62
    Time stamp doesn't seem to work....it seems like the path is too long now.
     
  7. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    OK good news, you are right and I have to give you good feedback, after many tests I can say that your original resize scaling algorithm is very good, you don't need to change anything. The only thing I had to change is MIPMAP= false (when true the image is blurry). Also I'm working now with 100x100.

    One issue left, how can I use it with your ImageCropper ?
    For obvious reasons, I'd like ImageCropper to crop the original size, not the 100x100...
    But then I don't know how to resize the cropped image, because ImageCropper resizing is not good, and also TextureScale.Bilinear is bad.
    Is it possible to use Native Gallery resizing algorithm as a function for the cropped image?
     
  8. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    30
    Hi,

    I am trying to compile a project for iOS using Native Gallery (using iOS project builder on my Windows) and I always have these compilation errors :

    Libraries/Plugins/NativeGallery/iOS/NativeGallery.mm(309,25): error: assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from incompatible type 'const Class'
    imagePicker.delegate = self;
    ^~~~
    Libraries/Plugins/NativeGallery/iOS/NativeGallery.mm(341,20): error: assigning to 'id<UIPopoverControllerDelegate> _Nullable' from incompatible type 'const Class'
    popup.delegate = self;
    ^~~~


    Is there someone who already have this issue ?
    Regards
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Suwas93 You can use
    System.DateTime.Now.ToString("yyyy-MM-ddTHH.mm") + ".png"
    (reference), it will be parsed to "2020-09-30T09.14.png". Perhaps your timestamp contained / or : characters which can be problematic in filepaths.

    @shlighter ImageCropper doesn't really have a resize algorithm like TextureScale. It just renders the camera to a RenderTexture of specified size and it automatically results in a scaled image. Perhaps modifying RenderTexture.GetTemporary's RenderTextureFormat would make a difference. To benefit from NativeGallery's native resize algorithm, you need to save the crop result as a high resolution PNG image and call LoadImageAtPath with that image.

    @FOKSlab I don't know if this issue is caused by iOS Project Builder asset but I've had no issues when building with Xcode so far. This is also the first time I'm seeing this error. Could you try building the project on a Mac device to see if the issue persists?
     
  10. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    30
    I will try that compiling on my Mac. Thanks for you quick answer. I will update all my MacOS/XCode before (and I will retry with iOS Project Builder too to see if MacOS and XCode versions matter).

    [With iOS Project Builder update no change ! Next step : try compiling with XCode]
     
    Last edited: Sep 30, 2020
  11. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    You mean to save the cropped image on the user's phone with NativeGallery.SaveImageToGallery and Load it again from path?
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    No, save it to temporaryCachePath with EncodeToPNG and File.WriteAllBytes and then load it again from that path. That is, if you want to use the native resize algorithm.
     
    swifter14 likes this.
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Suwas93 You can replace NativeGallery.jar with the attached one. It automatically adds timestamp to the filename if save operation fails.
     

    Attached Files:

    Suwas93 likes this.
  14. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    Hi @yasirkula
    When running on iOS (Unity 2019.4.10, XCode 12.0.1) I am getting this error at XCode:

    [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""

    Tracking it down, it occurs it occurs at NativeGallery.SaveToGalleryInternal at #elif !UNITY_EDITOR && UNITY_IOS
    _NativeGallery_ImageWriteToAlbum( path, album );

    (I got to SaveToGalleryInternal when calling NativeGallery.SaveImageToGallery (the string existingMediaPath overload)

    Can you please give some guidance what I may be doing wrong please?

    Thank you for your help !
     
  15. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    Do you have or can you make a YouTube tutorial how to isntal this package and how to use it, I want to make custom Avatar picture so player can use their own photo....

    Please can somebody create a YouTube video for the tutorial
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Tarrag If this error doesn't affect the plugin's functionality, I'd recommend you to ignore it. I also see similar messages but the plugins works fine for me. They may not be related to NativeGallery or might be an internal harmless Photos warning.

    @andi17dermawan I don't have a YouTube tutorial but I think you'd find the GitHub documentation useful. It contains example code, as well: https://github.com/yasirkula/UnityNativeGallery/
     
    Tarrag likes this.
  17. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    thanks sir, its work ...
    i just have a little problem,
    i try to change my object texture (500px/500px) using this code

    public void PickImage(int maxSize)
    {
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
    if (texture == null)
    {
    Debug.Log("Couldn't load texture from " + path);
    return;
    }
    Raw = GetComponent<RawImage>();
    Raw.texture = texture;
    }, "Select a PNG image", "image/png");

    Debug.Log("Permission result: " + permission);
    }

    the problem is when i choose pict with different height and weight it will be stretching ...
    do you have any solution sir ?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  19. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    Bro using my script above, did the images are saved to the app? And how can I access the image...
    Actually I want after the user select image from gallery, the image will be saved to my server...

    Can u help me ?
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    To upload the image to server, you must use UnityWebRequest or WWW. You can google "unity texture upload to server www" for example code.
     
  21. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    im trying to upload the texture to server but its cant i dont know where the error since i cant see the console on android....
    btw im using this code

    public Texture2D texture;
    RawImage Raw;

    public Text info;
    public void Button()
    {
    PickImage(512);
    }
    public void PickImage(int maxSize)
    {
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    texture = NativeGallery.LoadImageAtPath(path, maxSize);
    if (texture == null)
    {
    Debug.Log("Couldn't load texture from " + path);
    return;
    }
    Raw = GetComponent<RawImage>();
    Raw.texture = texture;
    }, "Select a PNG image", "image/png");

    Debug.Log("Permission result: " + permission);
    }

    public void UploadImage()
    {
    StartCoroutine(UploadImage2());
    info.text = "Loading";

    }

    IEnumerator UploadImage2()
    {
    WWWForm form = new WWWForm();
    byte[] textureBytes = texture.EncodeToPNG();
    info.text = texture.name;
    form.AddBinaryData("myImage", textureBytes, "myImage.png", "image/png");
    using (UnityWebRequest www = UnityWebRequest.Post(DBManager.URLUploadPP, form))
    {
    yield return www.SendWebRequest();
    // info.text = www.downloadHandler.text;
    Debug.Log(www.downloadHandler.text);
    }
    }


    i make the texture2D public so i can try to put manually image on there, and it s work perfectly my image is uploaded..
    but when i change the texture using native gallery, its not work
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  23. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    oh i find the error, it say : texture is not readable, the texture memory can bbe accessed from script. You can make the texture readable in the texture import setting

    how can i fix this problem sir ?
     
    Last edited: Oct 10, 2020
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Try
    NativeGallery.LoadImageAtPath(path, maxSize, false);


    P.S. Changed true to false.
     
  25. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    114
    I'm getting this error when I try to build for iOS after updteing to the new version (1.6)

    Assets\Plugins\NativeGallery\NativeGallery.cs(6,7): error CS0246: The type or namespace name 'NativeGalleryNamespace' could not be found (are you missing a using directive or an assembly reference?)​

    Any idea?

    PS, I'm also using ScreenshotHelper Plus and NativeShare which I updated as well. Hard to tell, but I think they are related and use commong libraries, right?
     
    Last edited: Oct 15, 2020
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  27. ekadiez

    ekadiez

    Joined:
    Mar 14, 2020
    Posts:
    1
    Hi, can you help me. How can i screenshot specific gameObject not the entire screen.
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi! It isn't really related to NativeGallery so you should google "unity partial screenshot" to find solutions to this problem.
     
    ekadiez likes this.
  29. RaventurnStefan

    RaventurnStefan

    Joined:
    Aug 1, 2012
    Posts:
    44
    Could you still get it to work with mac? Is there an update with iOS Projekt Builder?
    I bought iOS Project Builder today and got the same error.
     
  30. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    30
    There is a patch to apply on the Build.cmd file of iOS Builder, I will let the iOS Builder developer answer to you ( I sent him the thread link 5 min ago it should be quick ;) ).
     
    RaventurnStefan likes this.
  31. RaventurnStefan

    RaventurnStefan

    Joined:
    Aug 1, 2012
    Posts:
    44
    That sounds great. Thank you
     
  32. yathish9158

    yathish9158

    Joined:
    Apr 24, 2020
    Posts:
    5
    Hi, @yasirkula I created a new project using Native Gallery and NatCoder for recording video. From NatCoder I'll save the video to Application.persistentDataPath then I'm calling SaveVideoToGallery. For Android it's working, in IOS It won't save to gallery. Saving the image to the gallery in android and ios it's working.
    Record Function:
    Code (CSharp):
    1. public void StartRecording () {
    2.             // Start recording
    3.             var frameRate = 30;
    4.             var sampleRate = recordMicrophone ? AudioSettings.outputSampleRate : 0;
    5.             var channelCount = recordMicrophone ? (int)AudioSettings.speakerMode : 0;
    6.             var recordingClock = new RealtimeClock();
    7.             videoRecorder = new MP4Recorder(
    8.                 videoWidth,
    9.                 videoHeight,
    10.                 frameRate,
    11.                 sampleRate,
    12.                 channelCount,
    13.                 recordingPath => {
    14.                     Debug.Log($"Saved recording to: {recordingPath}");                  
    15.                     Debug.Log(videoPath);
    16.                     var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
    17.                     Handheld.PlayFullScreenMovie($"{prefix}{recordingPath}");
    18.                     NativeGallery.SaveVideoToGallery($"{prefix}{recordingPath}", "BabyEinstien", Path.GetFileName($"{prefix}{recordingPath}"));
    19.                 }
    20.             );
    21.             // Create recording inputs
    22.             cameraInput = new CameraInput(videoRecorder, recordingClock, Camera.main);
    23.             audioInput = recordMicrophone ? new AudioInput(videoRecorder, recordingClock, microphoneSource, true) : null;
    24.             // Unmute microphone
    25.             microphoneSource.mute = audioInput == null;
    26.         }
    MP4Recorder Function:
    Code (CSharp):
    1. public MP4Recorder (int width, int height, float framerate, int sampleRate, int channelCount, Action<string> recordingCallback, int bitrate = (int)(960 * 540 * 11.4f), int keyframeInterval = 3) {
    2.             var recordingDirectory = Application.persistentDataPath;
    3.             var recordingName = string.Format("recording_{0}.mp4", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff"));
    4.             if (width % 2 == 1 || height % 2 == 1)
    5.                 Debug.LogWarning("NatCorder Warning: Odd resolution might cause recording to fail");
    6.             switch (Application.platform) {
    7.                 case RuntimePlatform.OSXEditor:
    8.                     recordingDirectory = Directory.GetCurrentDirectory();
    9.                     goto case RuntimePlatform.OSXPlayer;
    10.                 case RuntimePlatform.WindowsEditor:
    11.                     recordingDirectory = Directory.GetCurrentDirectory();
    12.                     goto case RuntimePlatform.WindowsPlayer;
    13.                 case RuntimePlatform.OSXPlayer:
    14.                 case RuntimePlatform.WindowsPlayer:
    15.                 case RuntimePlatform.WebGLPlayer:
    16.                 case RuntimePlatform.IPhonePlayer: {
    17.                     var recordingPath = Path.Combine(recordingDirectory, recordingName);
    18.                     var nativeRecorder = MediaRecorderBridge.CreateMP4Recorder(width, height, framerate, bitrate, keyframeInterval, sampleRate, channelCount);
    19.                     this.recorder = new MediaRecorderiOS(nativeRecorder, width, height, recordingPath, recordingCallback);
    20.                     break;
    21.                 }
    22.                 case RuntimePlatform.Android: {
    23.                     var recordingPath = Path.Combine(recordingDirectory, recordingName);
    24.                     var nativeRecorder = new AndroidJavaObject(@"api.natsuite.natcorder.MP4Recorder", width, height, framerate, bitrate, keyframeInterval, sampleRate, channelCount);
    25.                     this.recorder = new MediaRecorderAndroid(nativeRecorder, width, height, recordingPath, recordingCallback);
    26.                     break;
    27.                 }
    28.                 default:
    29.                     Debug.LogError("NatCorder Error: MP4Recorder is not supported on this platform");
    30.                     break;
    31.             }
     
    Last edited: Oct 21, 2020
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Try this:
    NativeGallery.SaveVideoToGallery(recordingPath, "BabyEinstien", Path.GetFileName(recordingPath));
     
  34. EgorZhingerovskiy

    EgorZhingerovskiy

    Joined:
    Jul 6, 2020
    Posts:
    2
    Hello. On android 10+, when requesting for WRITE_EXTERNAL_STORAGE, popup with request not showing, and the plugin shows that access Granted.
    When writing to disk, the application crashed.

    BUT in asset https://github.com/yasirkula/UnityAndroidRuntimePermissions it doesn't happen and everything works as it should
     

    Attached Files:

  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Can you send me the crash logs? You can fetch them via adb logcat or Unity's Android Logcat package (easier).
     
  36. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    30

    Around line 960, after the block that checks "VerifyIPhoneSplashScreen", add a new block :


    Code (csharp):
    1.      rem // [2020-10-20] FIXME: Xcode's clang *silently allows* invalid assignations like these, while their *published* clang source code (quite correctly) throws an error like this:
    2.         rem // Libraries/Plugins/NativeGallery/iOS/NativeGallery.mm:304:25: error: incompatible pointer types assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from 'Class'
    3.         rem //         imagePicker.delegate = self;
    4.         rem //                                ^~~~
    5.         rem // Libraries/Plugins/NativeGallery/iOS/NativeGallery.mm:329:20: error: incompatible pointer types assigning to 'id _Nullable' from 'Class'
    6.         rem //                 popup.delegate = self;
    7.         rem //                                  ^~~~
    8.         rem // This behaviour is an UNPUBLISHED feature of Apple. As long as I don't have more clue about their intent, some hand patching is necessary.
    9.         if exist "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm" (
    10.                type "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm" | find "imagePicker.delegate = self;" > %TO_TEMPVAR%
    11.                if not "_!TEMPVAR!_"=="__" (
    12.                        rem // this NativeGallery plugin source file contains an invalid assignment and needs patching...
    13.                        copy /y /b "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm" "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm.BAK" > nul 2>&1
    14.                        "%TOOLCHAIN_PATH%\sed.exe" "s/delegate = self;/delegate = (id)self;/g" "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm.BAK" > "Libraries\Plugins\NativeGallery\iOS\NativeGallery.mm"
    15.                )
    16.         )
     
  37. RaventurnStefan

    RaventurnStefan

    Joined:
    Aug 1, 2012
    Posts:
    44

    Thanks for the support. I hope he can find a better solution for that. But that helps me a lot, thank you.
     
  38. yathish9158

    yathish9158

    Joined:
    Apr 24, 2020
    Posts:
    5
    Thank You for the help it worked.:)
     
  39. iliasM

    iliasM

    Joined:
    Dec 15, 2017
    Posts:
    15
    Hi yasirkua! You mention that you can " Prompt the user to pick multiple images or videos from Gallery/Photos (available on Android 18+ and iOS 14+)" I tap and hold an image for showing up the tick, to be able to choose more than one photos but I can't. Could you help me ?
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You need to use GetImagesFromGallery, not GetImageFromGallery.
     
  41. iliasM

    iliasM

    Joined:
    Dec 15, 2017
    Posts:
    15
    Thanks for answering me. I use NativeGallery.GetMixedMediaFromGallery. Is there any way to pick multiple files?
     
    Last edited: Nov 2, 2020
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    In this case, you should use GetMixedMediasFromGallery :D
     
  43. ArnaudJopart

    ArnaudJopart

    Joined:
    Oct 27, 2016
    Posts:
    13
    Hi there.

    First thanks a lot for this great asset. It saves me so much time.

    Since iOS14 and iPadOS14, when requesting for Photo access permission with NativeGallery.RequestPermission(), the user can now choose between three answers, the two good old ones (Don't allow, Allow access to all photos) and a third new one, "Select photos".

    I'm requesting permission at the start of my app, as it's AR based. I otherwise would risk loosing track of the AR object if I ask permission later (when the permission request pops up, the app looses focus, not updating the position of my GameObject).

    So, to solve this, requesting permission at start, instead of when needed. But then, if the user chooses "Select photos...", my app freezes.

    Any idea or workaround to solve this?

    Side note, it would be great to add a new value in the permission enum, for this "Select photos".

    Cheers
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is NativeGallery up-to-date in your project? If not, is the issue resolved after updating it?
     
  45. ArnaudJopart

    ArnaudJopart

    Joined:
    Oct 27, 2016
    Posts:
    13
    Hum... Forgot to check for update.
    All right, it's now up to date and working perfectly. Sorry for the trouble. Once again, thanks for your work!
     
    yasirkula likes this.
  46. dnlcl

    dnlcl

    Joined:
    Aug 12, 2020
    Posts:
    9
    Hi all

    I'm using this plugin because I need to take a screenshot and save it to the gallery (iOS/Android).
    I got 20 "Semantic Issue" building the Xcode project, my Xcode version is 12.1 and my Unity version is 2020.1.4f1.
    Instead in Android it works without any problem.
    I followed this manual setup: https://github.com/yasirkula/UnityNativeGallery/wiki/Manual-Setup-for-iOS
    Errors looks like this:


    Do you have any tip?
    Thanks
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    But why do you think that this issue is caused by NativeGallery?
     
  48. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    hi yazirkula...
    is there a way to get the image size (width & height) ? i want to put that size information to variables ...
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can use NativeGallery.GetImageProperties.
     
  50. andi17dermawan

    andi17dermawan

    Joined:
    Aug 21, 2020
    Posts:
    8
    is there more detail information on how to use NativeGallery.GetImageProperties ?