Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

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,910
    Permission is asked automatically when calling NativeGallery functions but it's called only once per function call. I'm still unfamiliar with this SIGSEGV error unfortunately.
     
  2. kanekoa12

    kanekoa12

    Joined:
    Aug 21, 2013
    Posts:
    3
    I've made sure that our code is not calling any code that would invoke a request permission. Seems there could be a a null pointer error. Here is more info:

    Fatal Exception: Tombstone: Version '2021.3.16f1 (4016570cf34f)', Build type 'Release', Scripting Backend 'il2cpp', CPU 'arm64-v8a'
    Build fingerprint: 'google/hatch/hatch_cheets:11/R112-15359.58.0/9891653:user/release-keys'
    Revision: '0'
    ABI: 'arm64'
    Timestamp: 2023-05-15 18:53:37-0700
    pid: 6871, tid: 7070, name: Thread-9 >>> com.XXX.XXX <<<
    uid: 10078
    signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1
    Cause: null pointer dereference

    #01 pc 0x4b15aa4 libil2cpp.so (NGPermissionCallbackAndroid.OnPermissionResult)

    Caused by java.lang.Error [Native Crash, signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)]: FATAL EXCEPTION [UnityMain]; Cause: null pointer dereference



     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
  4. SUUNE

    SUUNE

    Joined:
    Mar 10, 2019
    Posts:
    65
    Hey,
    Plugin works perfectly - thank you for your work.
    I have one question. I get ask for permission on app start, if I don't grant it then app will ask again when I call the method - works great, but how to prevent app from asking for permission on start? Permission is not needed on app start.
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
  6. donyayejadid

    donyayejadid

    Joined:
    Nov 24, 2020
    Posts:
    12
    just wanted to say thank you soooo much for this amazing plugin:)
    many kisses
     
    yasirkula likes this.
  7. danipomix

    danipomix

    Joined:
    Oct 18, 2019
    Posts:
    15
    Hi @yasirkula. First of all I wanted to compliment for nativegallery, which is a really amazing asset. Then I would like to ask you if there is a way to save a string value of the selected path on a variable in unity (or even on a variable in an external file for saving), DURING "theNativeGallery.GetImageFromGallery((path)"scene, BEFORE this gallery screen closes (so while nativegallery is running, and it put, I think, in the background the game).
    I'm asking because on Android when I launch this function, on return (when I select the image) the game sometimes crashes, and if I could save this value before this crash, I could directly reload the desired image after restarting the game with Texture2D t = NativeGallery.LoadImageAtPath(path, maxSize).

    *Right now, to try to save it I'm doing something like this, but it crashes before saving it:

    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    switch (path == null)
    {
    case false:

    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.persistentDataPath + "/CurrentPhotoPath.saved";
    FileStream stream = new FileStream(path, FileMode.Create);
    CurrentPhotoPath data = new CurrentPhotoPath(path);//stringvariable currentPhotoPath = path;
    formatter.Serialize(stream, data);
    stream.Close();

    return;
    }
    }, "Select a PNG image", "image/png");



    Thanks for your attention
     
    Last edited: Jun 10, 2023
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    Hmm, this can only be done by modifying the native source code in Android Studio. You can save unityResult to SharedPreferences and then create a function in NativeGallery.cs to read it back from SharedPreferences when you want.
     
    danipomix likes this.
  9. DMI84

    DMI84

    Joined:
    Jun 5, 2016
    Posts:
    6
    I dunno why, but my application start very long time (50+ seconds) with this plugin - black screen after unity logo - without plugin app start instantly. (Android 6)
     
    Last edited: Jun 20, 2023
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    Hmm, plugin doesn't do any sort of initialization as far as I'm unaware. You can check if any of the functions of the plugin are called at startup by you. Otherwise this shouldn't have happened.
     
  11. FreedLOW

    FreedLOW

    Joined:
    Dec 7, 2019
    Posts:
    15
    Hello, first, thanks for this plugin.
    I'm try to load image from gallery and then try to copy that image data to new one with readebla state (look at the code), but in Logcat i always get this error:
    Error Unity ArgumentException: Texture2D.GetPixels: texture data is either not readable, corrupted or does not exist. (Texture '')
    I'm eddit Android manifes and add this 2 lines:
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace" />
    But it's doesn't work and error alwayse display when get image from gallery. I hope u can help me with that.
    Thanks for attention!


    Code (CSharp):
    1.  NativeGallery.GetImageFromGallery(path =>
    2.             {
    3.                 var image = NativeGallery.LoadImageAtPath(path);
    4.                 if (image == null)
    5.                 {
    6.                     Debug.LogError("here not load image");
    7.                     return;
    8.                 }
    9.              
    10.                 if (image != null)
    11.                 {
    12.                     Texture2D readableTexture = new Texture2D(image.width, image.height);
    13.                     readableTexture.SetPixels(image.GetPixels());
    14.                     readableTexture.Apply();
    15.                  
    16.                     Debug.LogError("set readable format to texture and all should be fine");
    17.                     AddImage(readableTexture);
    18.                 }
    19.             });
     
    Last edited: Jun 26, 2023
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @FreedLOW Hi! You can simply set LoadImageAtPath's markTextureNonReadable parameter to false, no need to create a separate Texture at all.
     
  13. FreedLOW

    FreedLOW

    Joined:
    Dec 7, 2019
    Posts:
    15
    Thanks a lot, that help. But now I'm try to add this image to AR Library in tuntime, but not successful yet((
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @FreedLOW That doesn't exactly sound like it's related to NativeGallery. I don't know what AR Library means either so maybe it's just me.
     
  15. FreedLOW

    FreedLOW

    Joined:
    Dec 7, 2019
    Posts:
    15
    yep, that's not for you, thanks a lot)
     
    yasirkula likes this.
  16. FreedLOW

    FreedLOW

    Joined:
    Dec 7, 2019
    Posts:
    15
    Have one question for you, how can i get the name of image when i try get it from gallery like in my previous message? And should I delete this image after all operations?
     
    Last edited: Jun 26, 2023
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @FreedLOW Try calling this function in Awake:
    Code (CSharp):
    1. using( AndroidJavaClass ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeGalleryMediaPickerFragment" ) )
    2.     ajc.SetStatic<bool>( "tryPreserveFilenames", true );
    I wouldn't delete the selected image because it may point directly to the source image in Gallery, when possible. Or you can delete the image if its directory is temporaryCachePath.
     
  18. kanekoa12

    kanekoa12

    Joined:
    Aug 21, 2013
    Posts:
    3
    Thanks for the reply. Do you think it would be worth filing a bug with Unity for this?

     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @kanekoa12 Yes you can create an empty repro project and submit it via Help-Report a Bug menu. I really hope it gets resolved soon :)
     
  20. FreedLOW

    FreedLOW

    Joined:
    Dec 7, 2019
    Posts:
    15
    And how I can set tha name of image, using u example?
    string imageName = "name";

    Here is my code:

    Code (CSharp):
    1. NativeGallery.GetImageFromGallery(async path =>
    2.             {
    3.                 var image = await NativeGallery.LoadImageAtPathAsync(path, -1, false);
    4.                 if (image == null)
    5.                 {
    6.                     Debug.LogError("here not load image");
    7.                     return;
    8.                 }
    9.  
    10.                 string imageName = "name";
    11.                 ImageData imageData = new ImageData(imageName, image, 0.1f);
    12.                 if (imageDatas.Contains(imageData)) return;
    13.                 imageDatas.Add(imageData);
    14.            
    15.                 var random = Random.Range(0, 10);
    16.                 string address = random switch
    17.                 {
    18.                     < 5 => "TestCylinder",
    19.                     < 8 => "TestSphere",
    20.                     _ => "TestCube"
    21.                 };
    22.                 trackedImageManager.trackedImagePrefab = await assetProvider.Instantiate(address);
    23.            
    24.                 AddToLibrary();
    25.             });
    or i should just use:
    string imageName = System.IO.Path.GetFileNameWithoutExtension(path);
     
    Last edited: Jun 28, 2023
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @FreedLOW Path.GetFileNameWithoutExtension is correct.
     
    FreedLOW likes this.
  22. danipomix

    danipomix

    Joined:
    Oct 18, 2019
    Posts:
    15
    Hello Yasirkula. I tried to act on the Native gallery source code to make changes I wanted (saving the path value on sharedprefs during the async operation in the execute of NativeGalleryMediaPickerResultOperation) with android studio, (therefore compiling a new .aar to replace with the one present in my project, or even simply a .jar to replace the .jar inside the .aar), but it keeps giving me errors when I try to recompile it. Since I finally realized that I don't have the necessary skills to do this, could you recommend me a "HOW TO" where it explains how to recompile something like the source code of native gallery with android studio?
    Thanks, and sorry if this question is a bit off topic for this forum
     
    Last edited: Jul 10, 2023
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    danipomix likes this.
  24. wechat_os_Qy0_d5rjQ_em6jBpcn23QxyhQ

    wechat_os_Qy0_d5rjQ_em6jBpcn23QxyhQ

    Joined:
    Jul 13, 2023
    Posts:
    3
    Hello, Asilcula. I'm very sorry that I'm a beginner. I want to use the NativeGallery. GetMixedMediasFromGallery method, but I don't know how to use it. Can you provide me with an example or tutorial to take a look at? thanks
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @wechat_os_Qy0_d5rjQ_em6jBpcn23QxyhQ Of course:
    Code (CSharp):
    1. if( NativeGallery.CanSelectMultipleMediaTypesFromGallery() && NativeGallery.CanSelectMultipleFilesFromGallery() )
    2. {
    3.     NativeGallery.Permission permission = NativeGallery.GetMixedMediasFromGallery( ( paths ) =>
    4.     {
    5.         if( paths != null )
    6.         {
    7.             for( int i = 0; i < paths.Length; i++ )
    8.             {
    9.                 Debug.Log( i + "th media path: " + paths[i] );
    10.                
    11.                 // Determine if user has picked an image, video or neither of these
    12.                 switch( NativeGallery.GetMediaTypeOfFile( paths[i] ) )
    13.                 {
    14.                     case NativeGallery.MediaType.Image: Debug.Log( "Picked image" ); break;
    15.                     case NativeGallery.MediaType.Video: Debug.Log( "Picked video" ); break;
    16.                     default: Debug.Log( "Probably picked something else" ); break;
    17.                 }
    18.             }
    19.         }
    20.         else
    21.             Debug.Log( "Operation canceled" );
    22.     }, NativeGallery.MediaType.Image | NativeGallery.MediaType.Video, "Select images and/or videos" );
    23.  
    24.     Debug.Log( "Permission result: " + permission );
    25. }
     
  26. andischroff

    andischroff

    Joined:
    Sep 2, 2020
    Posts:
    1
    Thank you very much for the asset! I have two questions:
    1) Is it possible that iOS saving to an album does not work anymore after upgrade to iPadOS 16.5.1? It was working for me before the upgrade. Now saving works but no album is created.

    Code (CSharp):
    1. // Save the screenshot to Gallery/Photos
    2. NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(
    3.     loadedTexture, "GalleryMaker", "myTexture.png", (success, path) => Debug.Log("Media save! result: " + success + " " + path)
    4.     );
    5.  
    6. Debug.Log("Permission result: " + permission);
    2) If I know the filename and the album in iOS, can I load the image directly, i.e. without a user dialog?
     
    Last edited: Jul 19, 2023
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @andischroff

    1) It's actually documented: "On iOS 14+, the image/video will be saved to the default Photos album (i.e. album parameter will be ignored)". Writing to custom albums require full Photos access and it's not possible ask only that permission on iOS 14+. The permission dialog will have 3 options: "Deny, Selected Photos Only, Full Access". When user selects the 2nd option, it won't work. You may ask, why is there the 2nd option when I'm just attempting to save an image. You're absolutely right, I'm asking the same silly question myself to the iOS team.

    2) Not with NativeGallery unfortunately. I don't know the exact steps it'd require to accomplish this in the native code.
     
  28. duda_du

    duda_du

    Joined:
    Nov 20, 2019
    Posts:
    3
    Dear yasirkula, sorry for disturbing,there is a question that troubles me,Can “NativeGallery.GetImagesFromGallery()”limit the number of images to be selected? For example, I hope this method can only select 6 images。
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    duda_du likes this.
  30. duda_du

    duda_du

    Joined:
    Nov 20, 2019
    Posts:
    3
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    duda_du likes this.
  32. Jaapvd

    Jaapvd

    Joined:
    Nov 17, 2021
    Posts:
    4
     
  33. Jaapvd

    Jaapvd

    Joined:
    Nov 17, 2021
    Posts:
    4
    Hi, I've got two questions:
    (1) I use GetImageFromGallery and I would like that the app waits until the user has selected an image. I cannot find a simple solution to do that.
    (2) I would like to get access to the position where the picture was taken (latitude and longitude). Is there a way to achieve that?

    Thanks.
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @Jaapvd Hi, (1) is unfortunately not possible with this asset. If you grant Location permission before picking images, (2) may automatically be achieved but I'm not entirely sure. You'd need to give it a try.
     
  35. danipomix

    danipomix

    Joined:
    Oct 18, 2019
    Posts:
    15
    Hi Yasirkula. Thanks for the great tutorial you posted above, I was able to recompile the source code. Still acting on the source code, it is possible to make sure that when the user calls the unity-side function NativeGallery.GetImageFromGallery((path))(NativeGallery.PickMedia), only the photo gallery icon is displayed (so make sure that it is possible to open only the photo gallery), and not also the generic one (which also contains the videos) and the file manager? How can I handle it?
     

    Attached Files:

    Last edited: Aug 3, 2023
  36. duda_du

    duda_du

    Joined:
    Nov 20, 2019
    Posts:
    3
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    You can call the following code in Awake/Start to always use the default Gallery app on Android:
    Code (CSharp):
    1. #if UNITY_ANDROID
    2. using( AndroidJavaClass ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeGalleryMediaPickerFragment" ) )
    3.     ajc.SetStatic<bool>( "useDefaultGalleryApp", true );
    4. #endif
     
    danipomix likes this.
  38. Jaapvd

    Jaapvd

    Joined:
    Nov 17, 2021
    Posts:
    4
    tanks, I'll try.
     
    yasirkula likes this.
  39. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    Hello @yasirkula,
    I m trying to use NativeGallery to select multiple images from gallery but i failed.

    I m using the following. why I cant take the paths of the images. the file explorer gallery cannot open.

    Code (CSharp):
    1. NativeGallery.Permission permission = NativeGallery.GetImagesFromGallery((paths) =>
    2.         {
    3.             imagesPaths = paths;
    4.         }
     
    Last edited: Aug 8, 2023
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @crackbrain94 Your code looks good. Try updating the plugin via GitHub and check the value of permission in logcat. Also make sure that NativeGallery.CanSelectMultipleFilesFromGallery returns true.
     
  41. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    Thanks @yasirkula,

    On Unity editor NativeGallery.CanSelectMultipleFilesFromGallery() == false.

    NativeGallery.GetImageFromGallery works on editor
    Why NativeGallery.GetImagesFromGallery works only on Android?
     
  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @crackbrain94 The plugin doesn't support Editor. It's there for preview purposes only and is limited by Unity Editor's APIs. Unity doesn't provide an API for picking multiple files in Editor.
     
  43. luongbs94

    luongbs94

    Joined:
    Aug 2, 2018
    Posts:
    1
    why i receive this error : No overload for method 'GetImageFromGallery' takes 4 arguments

    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, false, false);
    if (texture == null)
    {
    Debug.Log("Couldn't load texture from " + path);
    Toast.instance.ShowMessage("Couldn't load texture");
    return;
    }
    createListItems.SetActive(false);
    createScreen.SetActive(true);
    createManager.LoadImage(texture);
    }
    }, "Select a PNG image", "image/png", maxSize);
    Debug.Log("Permission result: " + permission);
    }
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    @luongbs94 You should remove maxSize parameter from GetImageFromGallery. It must've been removed from that function quite some time ago.
     
  45. unityVankhai

    unityVankhai

    Joined:
    Apr 18, 2019
    Posts:
    4
    Please help me, I got this crash on Xiaomi MI 8 SE, but it's working fine with Red Mi Note 5, both have the same target api level 27.
    This is my logcat:
     

    Attached Files:

  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
  47. unityVankhai

    unityVankhai

    Joined:
    Apr 18, 2019
    Posts:
    4
    Debug ActivityTrigger activityResumeTrigger: The activity in ApplicationInfo{a4eaf87 android} is now in focus and seems to be in full-screen mode
    Error ActivityTrigger activityResumeTrigger: not whiteListedandroid/com.android.internal.app.ChooserActivity/27
    Debug ActivityTrigger ActivityTrigger activityPauseTrigger
    ...
    Warn Binder:839_4 type=1400 audit(0.0:41607): avc: denied { search } for name="26534" dev="proc" ino=2918941 scontext=u:r:audioserver:s0 tcontext=u:r:untrusted_app:s0:c512,c768 tclass=dir permissive=0
    ....
    ...
    Debug ActivityTrigger activityResumeTrigger: The activity in ApplicationInfo{a4eaf87 android} is now in focus and seems to be in full-screen mode
    Error ActivityTrigger activityResumeTrigger: not whiteListedandroid/com.android.internal.app.ChooserActivity/27
    Warn main type=1400 audit(0.0:41608): avc: denied { create } for name="cgroup.procs" scontext=u:r:zygote:s0 tcontext=u:eek:bject_r:cgroup:s0 tclass=file permissive=0
    Error SELinux seapp_context_lookup: No match for app with uid 1000, seinfo default, name android:ui
    Error SELinux selinux_android_setcontext: Error setting context for app with uid 1000, seinfo default:privapp:targetSdkVersion=27:complete: Success
    Error Zygote selinux_android_setcontext(1000, 0, "default:privapp:targetSdkVersion=27:complete", "android:ui") failed
    Fatal zygote64 jni_internal.cc:593] JNI FatalError called: frameworks/base/core/jni/com_android_internal_os_Zygote.cpp:652: selinux_android_setcontext failed
    Fatal zygote64 runtime.cc:598] Runtime aborting...

    @yasirkula thanks you for your attension.
    I think it has to be around these lines, on successfully perform device (Red Note 5), I don't see any denial, or this too : Error SELinux seapp_context_lookup: No match for app with uid 1000, seinfo default, name android:ui.
    It's weird because I can still access gallery using only Unity AnroidJavaObject,... but I can't get data from java thread without using onactivityresult and by doing that, it cause my app to crash
     
    Last edited: Aug 16, 2023
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,910
    May I see how you are accessing the gallery with AndroidJavaObject?
     
    unityVankhai likes this.
  49. unityVankhai

    unityVankhai

    Joined:
    Apr 18, 2019
    Posts:
    4
    Hi yasirkula, thank you for your help, It is indeed what you said, the problem is my device, I tried sharing image and it also cause crash too, not because of your plug-in, I will find a way to workaround it, sorry for the trouble. have a good day my man! About my code using AndroidJavaObject , it doesn't involve in any sharing method so it worked I think.
     
    yasirkula likes this.
  50. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    14
    Hello @yasirkula

    I m using the following to get multiple images from mobile devices.
    Code (CSharp):
    1. NativeGallery.Permission permission = NativeGallery.GetImagesFromGallery((paths) =>
    if the user is not allow the permission to access photo and video the 1st time then the code is not working.
    How can i show the allow or not allow window to the user every time the user trying.

    Thanks in advance.