Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Native Gallery for Android & iOS [Open Source]

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

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
  2. Forest3

    Forest3

    Joined:
    May 3, 2014
    Posts:
    14
    Hi Yasirkula, I was having this issue of having the pictures not showing correctly into albums, and the package you provided in this response seems to have solved it. Is there any reason why you haven't updated the asset to have that fix? Is that I should get the latest from GitHub?
     
    Last edited: Oct 30, 2019
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    That commit is already included in the Asset Store package. Probably a later change is causing the issue you are experiencing. There is a known issue with images being rotated 90 degrees on some Samsung Android devices' galleries. This happens when calling NativeGallery.SaveImageToGallery with the output of NativeCamera.TakePicture and I've recently released a fix for it: https://forum.unity.com/threads/native-camera-for-android-ios-open-source.529560/page-5#post-5114012. You can give it a try and see if the issue is resolved. Please let me know if that unitypackage fixes the issue.
     
  4. Julien_Houle

    Julien_Houle

    Joined:
    Feb 2, 2019
    Posts:
    1
    Hi, awesome library by the way!
    Just a thing, when i'm calling "GetImageFromGallery" it works but the first time i call it, it brings the opening image (the lock screen) of the cellphone, but doesn't close my application. Is there a way to avoid it? I might be missusing your library...
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    To clarify, does GetImageFromGallery simulate the behaviour of clicking the home button of the device at first call? Is this issue persistent across different devices? Are there any error messages in logcat? What does your code look like?
     
  6. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    50
    Hi I’m currently testing out this for my game and I have a few questions/suggestions.

    1. iOS album name folder. By looking at the iOS code it seems that you can save to a folder name but it requires Photo.framework. And in the post process script it removes the framework. So removing this it would work I suppose?

    2. When opening option settings in Android can we select if it’s in the settings opens inside the app or as a separate app (a new app activity).
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    1. Photos should be saved to the specified album on iOS. Is it not working or do you mean creating a sub-album? The latter is not supported.
    2. I'm guessing that you mean the new Settings Panel introduced in Android 10 (Q). AFAIK, they don't support displaying read/write permission settings for the time being: https://developer.android.com/reference/android/provider/Settings.Panel
     
  8. unitystefan

    unitystefan

    Joined:
    Mar 16, 2018
    Posts:
    9
    Hello @yasirkula
    Once again, great job with your plugins, they all are very helpful!
    I have one question regarding NativeGallery, when user opens gallery, is there are way to make user immediately start selecting multiple images (like when user holds finger at one and multiple selection starts), not just one and gallery closes when selecting it?

    Thanks in advance!
     
  9. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    50
    1. The image is only saved in recents Album. Would like if you could create a specific album and no lace them in there.

    2. What I wanted to explain is Settings opens inside the application and you need to use back key to return. Would be nice to open Settings as a separate app like how it’s done in iOS. I think there is a specific intent that can make this possible.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    @unitystefan I don't have any control over the image picker UI's behaviour. To be able to pick one or more images, you should call the NativeGallery.GetImagesFromGallery function. But it is supported on Android 18 and later only (no iOS support).

    @GustavNinja 1) seems like a bug because the image is supposed to be saved in the album that you specify, not in Recents album. Please open an Issue on the GitHub page with as much detail as possible and append any error messages to the Issue. 2) I'm not aware of this possible intent but please keep me posted if you find anything new about it.
     
  11. Slyer112

    Slyer112

    Joined:
    Sep 28, 2018
    Posts:
    8
    Hi great job for this asset I am wondering if I could pick the videos from a specific location instead of opening the recent tab > like for example the /storage/emulated/0/folder |


    the second thing can I view let's say a file full of videos in some kind of UI inside unity.
    again thx very much
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    It is not possible to specify an initial album/directory. You can do all sorts of things with the picked images/videos inside Unity. The example code shows how to display the picked image on a procedural cube, feel free to expand it to fit your needs.
     
  13. ctykaya

    ctykaya

    Joined:
    Sep 23, 2015
    Posts:
    12
    Hello yasirkula great plugin thanks for that. I have a problem. When I save my photo to gallery the orientation is not correct angle. How can ı fix it ?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
  15. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    50
    yasirkula. I looked at your code and made a Java script to test it out and it seems to work. If for example you want to open Application Details settings as a new Activity you can add the following code.
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     
  16. StacticKoud

    StacticKoud

    Joined:
    Nov 29, 2018
    Posts:
    5
    Is there an easy way to limit the length of the picked video? With the NativeCamera -plugin I can set the maximum recording length.
     
  17. Uxing

    Uxing

    Joined:
    Aug 31, 2019
    Posts:
    1
    yasirkula~~
    NativeGallery Works very smoothly
    But on Nokia8, 1 has failed.
    LogCat:
    2019-11-10 19:40:35.464 18265-18288/? E/Unity: UnauthorizedAccessException: Access to the path "/storage/emulated/0/GoVteam/10 - 11 - 2019_07 - 40 - 35.jpg" is denied.
    at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0
    at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <00000000000000000000000000000000>:0
    at System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) [0x00000] in <00000000000000000000000000000000>:0
    at NativeGallery.SaveToGallery (System.Byte[] mediaBytes, System.String album, System.String filenameFormatted, System.Boolean isImage, NativeGallery+MediaSaveCallback callback) [0x00000] in <00000000000000000000000000000000>:0
    at Takephoto_OnPostRender () [0x00000] in <00000000000000000000000000000000>:0
    I'm trying import the latest version from GitHub and try again.
    But Unity....tell me "Assets\Scripts\Takephoto.cs(343,47): error CS0117: 'NativeGallery' does not contain a definition for 'GetSavePath'
    "
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    @GustavNinja I'm glad that it is working for you but I still can't understand the difference. What behavioral or visual change did this tweak make on the plugin?

    @StacticKoud It is not possible, sorry.
     
  19. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    @yasirkula This plugin looks like just what I need. The only problem is I don't know how to start implementing it. All I need it to do is to allow the User to pick an image from there device and the application will store the directory path for the rest of my mechanics. How would I do this?
     
  20. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    50
    In my project they want both iOS and Android version to be similar so the game designers want the Option settings to open separately. That’s basically it.

    When it comes to the iOS Album problem. I renamed it and it showed up the old folders as well. I don’t know what might have caused it but my first folder was named “TestAlbum1”.
     
    yasirkula likes this.
  21. unitystefan

    unitystefan

    Joined:
    Mar 16, 2018
    Posts:
    9
    @yasirkula Too bad :( I'm already using NativeGallery.GetImagesFromGallery and multiple selection works as it should but problem is when user tap just at one photo it immediately proceed with just that one, so user must hold a tap and then multiple selection starts (which I want to start immediately without holding a tap)...
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    @NerdIt- You can check out the PickImage function in the example code: https://github.com/yasirkula/UnityNativeGallery/#example-code. I wouldn't recommend saving the path of the returned image directly since that image can possibly be overwritten on the next PickImage call. Instead, you can copy the picked image to Application.persistentDataPath with a unique name and save its path.

    @GustavNinja That is a weird bug but I'm not sure if it was caused by the plugin or by the device. If you can reproduce the issue later on, please try to determine the reprosteps and I'll take a look at it.

    @unitystefan It is out of my control, I'm afraid :/
     
  23. asyranamin

    asyranamin

    Joined:
    Nov 27, 2017
    Posts:
    7
    Hi @yasirkula love the plugin. Are you planning to update the plugin to select multiple images for ios?
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    iOS doesn't support this feature natively and I don't want to use a third party plugin for this task. So, I'm not planning to add this feature at the moment.
     
  25. DaVeF101

    DaVeF101

    Joined:
    Sep 7, 2014
    Posts:
    133
    errors building for Android..
    missing symbols: RELATIVE_PATH, DATE_TAKEN, IS_PENDING,
    method isExternalStorageLegacy()
    location: class Environment
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    5 errors

    help please?!

    using Unity 2018.3.8
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    I see, it is related to the used Android SDK version. I wasn't aware of this issue but will fix it soon. To fix the issue ASAP, you can use the latest Android Target API Level.
     
  27. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    31
    Hi. How do i set the image chosen onto a RawImage instead?

    Code (CSharp):
    1.  
    2. public RawImage myImageComponent;
    3.  
    4. void PickImage(int maxSize)
    5.     {
    6.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    7.         {
    8.             Debug.Log("Image path: " + path);
    9.             if (path != null)
    10.             {
    11.                 // Create Texture from selected image
    12.                 Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
    13.                 if (texture == null)
    14.                 {
    15.                     Debug.Log("Couldn't load texture from " + path);
    16.                     return;
    17.                 }
    18.  
    19.                 myImageComponent.transform.localScale = new Vector3(0.5f, texture.height / (float)texture.width, 0.5f);
    20.                 myImageComponent.texture = texture;
    21.              
    22.             }
    23.         }, "Select an image", "image/png");
    24.  
    25.     }
     
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    Your code looks correct to me. Only the scale could be an issue here but it will be fixed when you change 0.5f's to 1f. If your code is not working at all, make sure that myImageComponent is not null and that there are no error messages in Logcat.
     
  29. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    31
    Hey, so i got the image picker done, is it possible to set it as an array or list or something? coz i used that image picker code and set it into a RawImage prefab which is instantiated to a horizontal layout group. but i want the path for every image that it outputted.
     
  30. said123sawn

    said123sawn

    Joined:
    Oct 31, 2019
    Posts:
    16
    Hello, I can open the gallery, but I can not transfer the selected texture to the specified image.

    Can you help me
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    @ndmrzk To pick multiple images, you can use GetImagesFromGallery. The downside is that it works on only Android 18 and later, no iOS support.

    @said123sawn You can check out ndmrzk's code above, it transfers the selected texture to the specified RawImage.
     
  32. said123sawn

    said123sawn

    Joined:
    Oct 31, 2019
    Posts:
    16
    Thank you very much yasirkula:)
     
  33. said123sawn

    said123sawn

    Joined:
    Oct 31, 2019
    Posts:
    16
    well, can i play the audio file with this plugin
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    You can't play audio files. If you mean "picking" audio files, there are experimental functions for this in NativeGallery.cs. You can make those functions public and give them a go. But those function work on Android only.
     
  35. said123sawn

    said123sawn

    Joined:
    Oct 31, 2019
    Posts:
    16
    Ok,nevertheless thanks
     
  36. Javideas

    Javideas

    Joined:
    Jul 19, 2016
    Posts:
    3
    Hi yasirkula, I am 3d modeler with some code skills, I can read and write basic code and mix like a DJ code from others. So I wonder if would be difficult to make the next using your code:

    -load an image, from gallery, with a opacity slider (and scale) in top of the Camera view. The goal it’s to help trace drawings on paper (like a fake AR app...fake because I don’t need to use AR tech). I want to make a free ads app for Android and share it in the playstore....or at least I want it for personal use, and for my art friends
     
  37. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    31
    Hey @said123sawn how did u get the pick audio to work?
     
  38. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    31
    @yasirkula is it possible to give options to users to either pick image or video? Like for now image picker is like attached image. I assume video picker would be having the same UI. I was thinking if its possible to have the options together at the same time. Like "Select image/video", open gallery and both formats will appear. or perhaps, "Select file" then options Image Gallery, Video Gallery, (can we get rid of the Google photos option?)
     

    Attached Files:

    ashu39 likes this.
  39. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    31
    How to use the
    NativeGallery.GetVideoProperties(videoFilePath).rotation
    ? I got a red underline at rotation in visual studio. I want to get the video property, then if its a certain orientation, rotate it.
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    @Javideas You can create a Texture2D from the picked image via LoadImageAtPath and assign it to a RawImage. Then, you can decrease the RawImage's opacity (color.a) to make it translucent.

    @ndmrzk To be honest, I'm not planning to create a native dialog similar to the one you've shown to prompt the user to select what to pick, it is not a common use-case. You can create a UI for this in Unity. To get rid of the Google Photos option, you can change this line as follows and rebuild NativeGallery.jar:
    startActivityForResult( intent, MEDIA_REQUEST_CODE );


    About the second question, are you trying to assign the rotation to a Vector3? If so, you should probably assign it to the vector's z component.
     
  41. TheFractor

    TheFractor

    Joined:
    Sep 9, 2019
    Posts:
    2
    @yasirkula thanks for your library.

    A) how i can know the real image/video path when i get one of them ?

    B ) i try to use the code for videoplayer ... but this is the log : AndroidVideoMedia: Error opening extractor: -10002
    device : mi9t pro / os : android 11.0.3 / media : file.mp4
    Code (CSharp):
    1.  public void PickVideo()
    2.     {
    3.         NativeGallery.Permission permission = NativeGallery.GetVideoFromGallery((path) =>
    4.         {        
    5.                 Text.SetText("Video path: " + path);
    6.                 if (path != null)
    7.                 {
    8.                     // Play the video on a cube
    9.                     GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    10.                     cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
    11.                     cube.transform.forward = -Camera.main.transform.forward;
    12.  
    13.                     Material material = cube.GetComponent<Renderer>().material;
    14.                     if (!material.shader.isSupported) // happens when Standard shader is not included in the build
    15.                         material.shader = Shader.Find("Legacy Shaders/Diffuse");
    16.  
    17.                     AudioSource audioSouce = cube.AddComponent<AudioSource>();
    18.                     VideoPlayer videoPlayer = cube.AddComponent<VideoPlayer>();
    19.                     videoPlayer.renderMode = VideoRenderMode.MaterialOverride;
    20.                     videoPlayer.targetMaterialRenderer = cube.GetComponent<Renderer>();
    21.                     videoPlayer.targetMaterialProperty = "_MainTex";
    22.                     videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    23.                     videoPlayer.EnableAudioTrack(0, true);
    24.                     videoPlayer.SetTargetAudioSource(0, audioSouce);
    25.  
    26.                     videoPlayer.url = path;
    27.                     videoPlayer.source = VideoSource.Url;
    28.                     videoPlayer.Play();
    29.                     audioSouce.Play();
    30.                     disableCanvas();
    31.                     //Handheld.PlayFullScreenMovie("file://" + path);
    32.                 }
    33.          
    34.         }, "Select a video");
     
    Last edited: Nov 27, 2019
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    A) 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), on Android, with Storage Access Framework, the absolute path is hidden behind the SAF-layer. There are some tricks here and there to convert SAF-path to absolute path but they don't work in all cases (most of the snippets that can be found on Stackoverflow can't return the absolute path if the file is stored on external SD card).

    B) VideoPlayer can return all sorts of error messages. I think this one is caused by Unity rather than NativeGallery: https://forum.unity.com/threads/error-videoplayer-on-android.742451/#post-5105759
     
  43. TheFractor

    TheFractor

    Joined:
    Sep 9, 2019
    Posts:
    2
    12/5000


    Thanks so much
     
  44. Beakmanson

    Beakmanson

    Joined:
    Aug 26, 2013
    Posts:
    2
    Hello yasirkula! Thank you for providing this utility, really appreciated!
    I'm having an issue understanding how to use it properly, because I can't get the result I want. In my android application I want the user to pick an image from the gallery, and store it in the Application.persistentDataPath. That way, the next time the user opens the app, I can load the picture directly without going directly through the gallery. The problem I'm facing is that, no matter how I write the code, I can't save the picture to the persistentDataPath. I'm using the example from your github, modified to fit my needs, and it looks like this:

    Code (CSharp):
    1. Texture2D newTexture = new Texture2D(2, 2);
    2. NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    3.         {
    4.             textLabel.GetComponent<Text>().text = "Path name: " + path;
    5.             if (path != null)
    6.             {
    7.                 // Create Texture from selected image
    8.                 newTexture = NativeGallery.LoadImageAtPath(path, 512);
    9.                 if (newTexture == null)
    10.                 {
    11.                     Debug.Log("Can't create the texture");
    12.                     return;
    13.                 }
    14.          
    15.                 byte[] textureBytes = newTexture.EncodeToJPG();
    16.                 path = Application.persistentDataPath + "/Test01.jpg";
    17.                 File.WriteAllBytes(path, textureBytes);
    18.             }
    19.             else
    20.             {
    21.             Debug.Log("No path");
    22.             }
    23.         }, "Select a JPG image", "image/jpg");
    I have tried many many variations of this code and none of them work. Either I end up with not being able to write the texture, or it writes a 0 bytes file (checking it, it seems that when I try to read the raw data of the picture it returns an empty byte[])

    I'm completely lost here. Do you have any idea or suggestion of what I might be doing wrong? (Or anyone else)
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    You don't have to create a Texture in this case. Simply call
    System.IO.File.Copy(path,Application.persistentDataPath + "/Test01.jpg",true);
    inside
    if(path != null)
    to copy the picked image to the desired location.
     
  46. Beakmanson

    Beakmanson

    Joined:
    Aug 26, 2013
    Posts:
    2
    Well, that was easy... thank you for your help and for the great utility!
     
    yasirkula likes this.
  47. milamila

    milamila

    Joined:
    Nov 21, 2013
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SaveToGallery : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public void SaveVideoToGallery()
    10.     {
    11.      
    12.         NativeGallery.SaveVideoToGallery(System.IO.Path.Combine(Application.persistentDataPath, "video1.mp4"), "Wines!!!", "video1.mp4");
    13.     }
    14.  
    15.    
    16. }
    17.  
    Dear yasirkula, sorry for disturbing, but being a little new to coding i can not do the following:
    1. i have a videofile in my app and a download button on it, which successfully downloads my video to persistent data path.
    2. Then appears a button "Save to gallery" and i thought that with the help of your plugin by pressing it i coud have a new folder in the gallery with this video inside and that it also will not ovewright next download, but ask for a new name.

    Is it possible? How my script must look like? I understand very well that this is a very stupid question and i am sorry again for waisting your time, but still hope for your help.
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    Your code looks perfect, assuming that video1.mp4 exists at persistentDataPath. I'm not sure if exclamation marks are allowed in album names, though. Please note that NativeGallery won't prompt the user to enter a unique name for the saved video file, it will automatically give the video a unique name if a video with the same name already exists in the specified album.
     
  49. solmyr-fr

    solmyr-fr

    Joined:
    Jun 18, 2009
    Posts:
    20
    Hi !

    Thanks a lot for this plugin, it's a great time-saver !

    Is there a way to get the default gallery full-path of an album (on both android and ios) ? I didn't find an utility function in the code/doc to do it.

    In Standalone it's easy to hardcode it (with Application.persistentDataPath but doesn't work in mobile if you want the image to appear in the gallery thus the use of your great plugin)

    EDIT :
    - I could hardcode it (like Kardux's answer here : https://stackoverflow.com/questions...pears-after-updating-an-app/39994912#39994912) but it's only in Android (what is the default storage folder for ioS ?) and it's a little ugly/lazy (and probably doest not cover all phone models).
    - I could force the user to select an existing album to get the path (using the callback of "GetImageFromGallery"). But it change the game-design of my app, forcing a new step that is purely technical without any purpose for the user)
    - Any other idea ?


    Thanks.
     
    Last edited: Dec 9, 2019
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    1,726
    Unfortunately, you can't get the path. More info from the FAQ:
     
unityunity