Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    2,864
    That's not possible, sorry.
     
  2. suitmrt

    suitmrt

    Joined:
    May 19, 2016
    Posts:
    8
    Hi, yasirkula!
    Problem solved.
    I'm sorry for this question. It wasn't even a freeze :( The problem was in changing gameObject's order in hierarchy after unparenting it (it would block everything else from clicks). I was searching problem solution in wrong places, code and logs, should have taken a look into a hierarchy :) My bad.
     
    yasirkula likes this.
  3. Taurenvd

    Taurenvd

    Joined:
    May 20, 2016
    Posts:
    2
    Same problem as #287. Cant create GO helper for callback. 2018.3.2.1f
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
  5. Taurenvd

    Taurenvd

    Joined:
    May 20, 2016
    Posts:
    2
    yasirkula likes this.
  6. fschaar

    fschaar

    Joined:
    Jul 3, 2012
    Posts:
    14
    Hi,

    I know I asked that before, but I want to use Native Gallery, to get audio files from Android - not actually the real files, more the metadata: Title URI and such.

    That should be possible with GetVideosFromGallery. If I use an asterix for mime type, it should give back everything and i can figure out the Audio files right?
    I still don't understand how to use it. I mean, it works asynchronous? How can I determine, when it's ready?

    I tried a code like this:
    Code (CSharp):
    1.  public void ShowAudios()
    2.  
    3.     {
    4.         NativeGallery.MediaPickMultipleCallback callback = new MediaPickMultipleCallback(HandleMediaPickMultipleCallback);
    5.         NativeGallery.GetVideosFromGallery((callback, "", "*");
    6.      
    7.     }
    But I don't know how to go from there. Does it return strings? or do I get a picker(not intended)?

    Maybe I'm to dumb on that, but I don't see from your description and code example how to get a simple string list of the available media on the device.
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    Hi,

    It is not possible to fetch the list of media on the device. NativeGallery returns only the media that user explicitly picks. Paths to the picked media are passed in a string[] parameters to the callback function. The process is asynchronous, yes.

    P.S. Picking audio files is not officially supported, changing the mime may not work on its own.
     
  8. XavierFoley

    XavierFoley

    Joined:
    Dec 25, 2018
    Posts:
    1
    Hi
    I have used Native Gallery recently, but I can't save captured image from camera into mobile gallery in iPhone using Vuforia.
    Android works well, but iPhone does not work.
    Code is below:
    IEnumerator SaveToGallery(string strFileName)
    {
    yield return new WaitForSeconds(1f);
    var fileName = Application.persistentDataPath + "/" + strFileName;
    var bytes = File.ReadAllBytes(fileName);
    var tex1 = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
    tex1.LoadImage(bytes);
    Debug.Log("=== Bytes " + bytes.Length);
    //NativeGallery.SaveImageToGallery(bytes, "Live Color 3D", strFileName, null);
    //NativeGallery.SaveImageToGallery(fileName, "Live Color 3D", strFileName, null);
    NativeGallery.SaveImageToGallery(tex1, "Live Color 3D", strFileName, null);
    }
    I got Callback Error below:
    The operation couldn’t be completed. (Cocoa error -1.)

    What's the matter?
     
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    Is the return value of SaveImageToGallery equal to Permission.Granted? Do the other two SaveImageToGallery variants also yield the same error? Does strFileName contain the extension of the video file, as well (e.g. '.mp4')? Is there enough free storage space? Is restarting your device any helpful?
     
  10. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    Hello bro, thanks for this pluggin, i have implemented very well, i just have two error, when i pick an image (through a button) it appear the next https://imgur.com/a/ju4u04l i dont know if this is a bug but, it works if i press "Album" button on the left corner it appear all the images, is there a way that , when i press the button to take a picture all my albumes appear? (and not as the image) the other things is, how can i "erase" or unable the camera button from the righ corner? becouse when i press it, the app crashes, thanks for your help
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    That looks weird. Can I see your code?
     
  12. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    This is the code bro

    Code (CSharp):
    1. public class pickfromgallery : MonoBehaviour {
    2.  
    3.     public RawImage rawImg;
    4.     public AspectRatioFitter fit;
    5.    
    6.  
    7.     public void Pick()
    8.     {
    9.         // Don't attempt to use the camera if it is already open
    10.         if (NativeGallery.IsMediaPickerBusy())
    11.             return;
    12.         PickImage(1024);
    13.     }
    14.  
    15.  
    16.  
    17.     private void PickImage(int maxSize)
    18.     {
    19.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    20.         {
    21.             Debug.Log("Image path: " + path);
    22.             if (path != null)
    23.             {
    24.                 // Create Texture from selected image
    25.                 Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
    26.                 if (texture == null)
    27.                 {
    28.                     Debug.Log("Couldn't load texture from " + path);
    29.                     return;
    30.                 }
    31.  
    32.                 float ratio = (float)texture.width / (float)texture.height;
    33.                 fit.aspectRatio = ratio;
    34.                 rawImg.texture = texture;
    35.  
    36.             }
    37.         }, "Select a PNG image", "image/png", maxSize);
    38.  
    39.         Debug.Log("Permission result: " + permission);
    40.     }
    41.  
    42. }
     
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    Couldn't see anything wrong in your code. As a workaround, you can try adding a Camera Usage Description to Info.plist in Xcode to resolve the crash.
     
  14. zelmax

    zelmax

    Joined:
    Mar 16, 2018
    Posts:
    10
    Bro, I tried to run the app in a modern phone, there is no problem, gallery launches so good, the problem is in old phones, so the gallery is resolved, but for the camera? How can I do to unable on old phones while Im using gallery?
     
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    I couldn't find a way to hide that camera button. While testing it myself, I didn't notice a camera button, so maybe it is a phone-specific thing, as well. Try applying the workaround I've mentioned in my previous message to at least prevent the crash from occurring.
     
  16. alienkani

    alienkani

    Joined:
    Nov 15, 2017
    Posts:
    3
    Hi Yasirkula this is an amazing plugin i appreciate your work. can you please tell me how can open image in gallery not in Unity application. i wanna save image and then open it in gallery.
     
  17. moaz0004

    moaz0004

    Joined:
    May 22, 2017
    Posts:
    8
    GetImageFromGallery function is not returning the path. It an empty string.

    This is the code.
    public void AddProfilePicFromGallery()
    {
    if (NativeGallery.IsMediaPickerBusy())
    return;
    PickImage(512);
    //NativeGallery.GetImageFromGallery( NativeGallery.MediaPickCallback("asd") { },)
    }
    private void PickImage(int maxSize)
    {
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    Debug.Log("Image path: " + path);
    if (path != null)
    {
    Debug.Log("path is not null.");

    // Create Texture from selected image
    Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
    if (texture == null)
    {
    Debug.Log("Couldn't load texture from " + path);
    return;
    }


    ProfilePic.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one / 2, 100); ;

    //Destroy(texture, 5f);
    }
    }, "Select a image", "image/*", maxSize);

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

    moaz0004

    Joined:
    May 22, 2017
    Posts:
    8
    Solved: I didn't add the permission in manifest file previously.
     
    yasirkula likes this.
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
  20. magglemitch

    magglemitch

    Joined:
    Dec 8, 2013
    Posts:
    110
    Is there an ability to pull in low-res versions of the photos from the gallery? Grabbing photos and applying them as textures create a bit of noticeable lag when the photos are first loaded (due to their size I bet).
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    NativeGallery.LoadImageAtPath has a property called maxSize that should do the trick.
     
  22. judokalexx

    judokalexx

    Joined:
    Dec 18, 2018
    Posts:
    1
    Hey! Very great work and the all for free!! Thank you so much...
    But i have never used this type of script and I don’t understand at all how I must do for take a photo from the gallery in my script...
    if it is possible that you help me it would be very nice !
     
    yasirkula likes this.
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
  24. Haseeb59

    Haseeb59

    Joined:
    Aug 7, 2017
    Posts:
    4
    Hey Yasir !
    Awsome plugin .
    I am having an issue I want to pick image from gallery and then convert that picked texture to bytes and send that byte array to my server so that my image will be saved on my server.
    Everthing is working good ! Except I am unable to convert the texture to bytes that I get from my android phone gallery. I am habing this issue in android phone.
    i am using this code kindly have a look
    Thanks



    public void PickImage(int maxSize)
    {
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    Debug.Log("Image path: " + path);
    if (path != null)
    {
    // Create Texture from selected image
    texture = NativeGallery.LoadImageAtPath(path, maxSize);
    if (texture == null)
    {
    Debug.Log("Couldn't load texture from " + path);
    UpdateErrorMessage("Image not picled", Color.red);
    return;
    }
    // The inage is rendering on a rawImage
    image.texture = texture;

    // This is where I am having issue while converting it to bytes
    byte[] imagebytes = texture.EncodeToPNG();


    //Destroy(texture, 5f);
    }
    }, "Select a PNG image", "image/png", maxSize);

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

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    Use
    texture = NativeGallery.LoadImageAtPath(path, maxSize, false);
     
  26. GeorgeMincila

    GeorgeMincila

    Joined:
    Feb 28, 2016
    Posts:
    36
    Hi,

    Thank you for this resource. I am trying to use it in my Android app to save a screenshot to the Gallery using the code below. However, the result that I can see is Permission result: Denied.

    Anything extra needs to be done besides importing the Unity Package?

    Code (CSharp):
    1.     public void tappedSnapshot()
    2.     {
    3.         AndroidManager.HapticFeedback();
    4.         StartCoroutine(TakeScreenshotAndSave());
    5.  
    6.     }
    7.     private IEnumerator TakeScreenshotAndSave()
    8.     {
    9.         yield return new WaitForEndOfFrame();
    10.  
    11.         Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    12.         ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    13.         ss.Apply();
    14.  
    15.         // Save the screenshot to Gallery/Photos
    16.         Debug.Log("Permission result: " + NativeGallery.SaveImageToGallery(ss, "GalleryTest", "My img {0}.png"));
    17.  
    18.         // To avoid memory leaks
    19.         Destroy(ss);
    20.     }
    *also in code "NativeGallery" is highlighted in red everywhere and it says: "The name NativeGallery does not exist in the current context"
     
    Last edited: Mar 6, 2019
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    You should set Write Permission to External (SDCard) in Player Settings. I am not sure why NativeGallery is highlighted in red in your VS, though.
     
  28. GeorgeMincila

    GeorgeMincila

    Joined:
    Feb 28, 2016
    Posts:
    36
    Hm. I have just done the change you suggested, but the result is the same. Permission Denied.

    It is weird that code is highlighted in red, like I mentioned. https://imgur.com/a/Uk2KxRn
    However it does compile and app runs.
     
  29. brackinz

    brackinz

    Joined:
    Mar 20, 2018
    Posts:
    8
    I'm having the same issue, trying to save a texture2D to the gallery but it's giving me permission result denied. Tested the save screenshot example too.
    Also, when trying to use RequestPermission it doesn't do anything.
     
  30. brackinz

    brackinz

    Joined:
    Mar 20, 2018
    Posts:
    8
    Found the main issue, the HockeyApp plugin is interfering with the permissions
     
    yasirkula likes this.
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    @GeorgeMincila Does the plugin work correctly on a new Unity project (after setting the Write Permission)? Please try in both Gradle build mode and Internal (Legacy) build mode.

    @brackinz Thanks for the information! I couldn't figure out how that plugin could interfere with mine but thanks anyways.
     
  32. GeorgeMincila

    GeorgeMincila

    Joined:
    Feb 28, 2016
    Posts:
    36
    @yasirkula I just tested in a new project and it works fine in that case.

    In my project, I always get Permission denied.. I have to mention my project is using ARFoundation. Could this be at fault?
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    I'm not sure. If ProGuard is enabled in your project, try adding the following line to ProGuard filters:

    -keep class com.yasirkula.unity.* { *; }
     
  34. GeorgeMincila

    GeorgeMincila

    Joined:
    Feb 28, 2016
    Posts:
    36
    Hi @yasirkula , thanks for the idea. I do not think that is the cause here. Let me share some more findings, in case any idea comes to mind. As I mentioned I am working with ARFoundation. Unity sample project here.

    - ARFoundation, sample project, AR scene, use snapshot code: Freezes a bit, getting Permission Denied, nothing else happens, app continues to work.
    - ARFoundation, sample project, new blank scene that does not have AR, use snapshot code: I am getting asked for permission, image saved successfully in the gallery.
    - ARFoundation, sample project, AR scene AGAIN, but after previous point (where I got permissions), use snapshot code, image gets captured and saved fine in the gallery.

    So the issue is that the permissions dialogue does not get shown while in AR, because it returns Permissions = Denied. If I manually display the permissions dialogue, even if Permission is denied, it does show up. I do however fear for the app on the Play Store if I keep asking for permissions, even if the user denied it.
     
    Last edited: Mar 14, 2019
  35. Abdul_M

    Abdul_M

    Joined:
    Feb 17, 2018
    Posts:
    11
    @yasirkula how do I access the android gallary?
    on unity i use
    filePath = EditorUtility.OpenFilePanelWithFilters("Select .jpg selfie photo", "", new string[] {
    "All Files",
    "jpg,jpeg,png,pdf,pptx"
    });

    but what is the file path on the Native Gallery
     
  36. Abdul_M

    Abdul_M

    Joined:
    Feb 17, 2018
    Posts:
    11
    used this piece of code:
    private void PickImage( int maxSize )

    and called in a button function but didnt work
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    @GeorgeMincila That's very valuable feedback, thanks. Currently, I don't know why this happens but I will update the package if I manage to resolve the issue. BTW, you don't have to worry about permission getting asked continuously even if it is Denied, permission dialog can't be shown if user selects "Don't ask again".

    @Abdul_M Please see the example code to see how to get the path of the selected image/video: https://github.com/yasirkula/UnityNativeGallery#example-code

    You may also be interested in https://forum.unity.com/threads/simple-file-browser-open-source.441908/
     
  38. Abdul_M

    Abdul_M

    Joined:
    Feb 17, 2018
    Posts:
    11
    @yasirkula thanks, I've followed your code example and made the PickImage Class public. I assigned it to a button, when i click the button on the android device, nothing happens... not sure if it works like that, as i am running it on the Android device
     
  39. Abdul_M

    Abdul_M

    Joined:
    Feb 17, 2018
    Posts:
    11
    I see what i did wrong now, I was accessing the internal files, so for those using unity and are junior like me. The code will only work if you go to Player Setting > Android > Other Setting > (under) Configuration > Write Permission. change Write Permission to External (SDCard) and it will work. Which of course is mentioned in the ReadMe for Yasirkula but, not in detail as i did not know where it was :p
     
    yasirkula likes this.
  40. Haseeb59

    Haseeb59

    Joined:
    Aug 7, 2017
    Posts:
    4
    Hi ! Yasir great assets !
    I have a question
    1- Can I save a texture 2d to gallery (not persistent path) using this plugin ? Waiting for reply !

    I am able to get the image from gallery but its opposite is not working
    I am using this code


    public void DownloadImage()
    {
    texture = RefernceImage.sprite.texture;

    string filePath = Path.Combine(Application.persistentDataPath, "OurNewImg.png");
    File.WriteAllBytes(filePath, texture.EncodeToPNG());
    print(filePath);

    print(NativeGallery.SaveImageToGallery(filePath , "TopTellpix" , "TopTellpix "+ System.DateTime.Now));

    }


    Thanks
     
    Last edited: Mar 21, 2019
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    You can simply use
    NativeGallery.SaveImageToGallery(texture, "TopTellpix" , "TopTellpix "+ System.DateTime.Now);
     
  42. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    274
    Hello.
    I'm using Unity 2018.3.0f1
    I have an issue when building my app in release mode (this bug does not exists when I build in Development mode).
    When calling the NativeGallery.RequestPermission() I have an error (I found it thanks to your in game debug console plugin !!)
    Image of the error and my project view.

    project.png

     
    Last edited: Mar 25, 2019
  43. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    274
    Ok I found a solution. It was because I used Minify(Gradle) instead of None !
     

    Attached Files:

  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    In minify mode, you need to exclude classes in com.yasirkula.unity package. In ProGuard, this is done by adding the following line to ProGuard filters:

    -keep class com.yasirkula.unity.* { *; }
     
  45. corrivai

    corrivai

    Joined:
    Mar 14, 2016
    Posts:
    12
    Hello, i'm using Unity 2018.3.11f1.
    I have the same problem as @Fangh
    I changed the setting to Minify, but I didn't fix it.
    These are my settings:

    Code:
    Code (CSharp):
    1.  var permission = NativeGallery.CheckPermission(); //Error
    2.             Debug.Log("NativeGallery Permission: " + permission.ToString());
    3.  
    4.             switch (permission)
    5.             {
    6.                 case NativeGallery.Permission.Denied:
    7. //
    8.                     break;
    9.                 case NativeGallery.Permission.ShouldAsk:
    10. //
    11.                     break;
    12.                 case NativeGallery.Permission.Granted:
    13.  
    14.                     var canMultiple = NativeGallery.CanSelectMultipleFilesFromGallery();
    15.                     Debug.Log("Multiple files: " + canMultiple);
    16.  
    17.                     if (canMultiple)
    18.                         NativeGallery.GetImagesFromGallery(GalleryFile_Callback, "Get files");
    19.                     else
    20.                         NativeGallery.GetImageFromGallery(GalleryFile_Callback, "Get file");
    21.  
    22.                     break;
    23.             }
    1.PNG 2.PNG 3.PNG

    Error:

    Code (CSharp):
    1. java.lang.ClassNotFoundException: com.yasirkula.unity.NativeGallery
    2. at java.lang.Class.classForName(Native Method)
    3. at java.lang.Class.forName(Class.java:453)
    4. at java.lang.Class.forName(Class.java:378)
    5. at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    6. at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
    7. at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
    8. at android.os.MessageQueue.next(MessageQueue.java:395)
    9. at android.os.Looper.loop(Looper.java:165)
    10. at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
    11. Caused by: java.lang.ClassNotFoundException: Didn't find class "com.yasirkula.unity.NativeGallery" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.comp.appname-LRmJRNv51jA8np2nACQ8Ag==/base.apk"],nativeLibraryDirectories=[/data/app/com.comp.appname-LRmJRNv51jA8np2nACQ8Ag==/lib/arm, /data/app/com.comp.appname
    How i can exclude classes in com.yasirkula.unity package?
     
    Last edited: Apr 10, 2019
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    I'm not sure how Gradle minification works. You can switch to ProGuard minification and then add
    -keep class com.yasirkula.unity.* { *; }
    to its filters.
     
  47. corrivai

    corrivai

    Joined:
    Mar 14, 2016
    Posts:
    12
    Okay, I didn't understand where to write
    -keep class com.yasirkula.unity.* { *; }
    .
    This is my settings:

    4.PNG

    Now It works, thank you.
     
    yasirkula likes this.
  48. Entsking

    Entsking

    Joined:
    Apr 29, 2018
    Posts:
    4
    Hi i'm trying to select a image from the galery to change an image on my aplication by precing a button, i tryed a lot of things but didnt work, even your exemple code. i dont know if its the version of the unity or the android, the code is (i know the code isnt rigth but i'm have problem even to show/open the gallery in my aplication using all the exemples)

    Code (CSharp):
    1. public Texture2D pickedImage;
    2. public GameObject Image;
    3.  
    4. public void ChangeImage ()
    5. {
    6. PickImagefronGallery()
    7. //Change the Image Object
    8. }
    9.  
    10. public void PickImageFromGallery( int maxSize = 1024 )
    11. {
    12.     NativeGallery.GetImageFromGallery( ( path ) =>
    13.     {
    14.         if( path != null )
    15.         {
    16.             // Create Texture from selected image
    17.             pickedImage = NativeGallery.LoadImageAtPath( path, maxSize );
    18.         }
    19.     }, maxSize: maxSize );
    20. }
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,864
    Is Write Permission set to External (SDCard)?
     
  50. Entsking

    Entsking

    Joined:
    Apr 29, 2018
    Posts:
    4
    that's the problem i'm receiving no messanges or permission request. I saw some comments about permission in android before version 7, my android it's 7.1.1. Thanks for your fast reply