Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    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,799
    @crackbrain94 Hi! The permission dialog can be displayed only once on iOS. This limitation is put by the operating system. User must visit Settings to grant the permission later.
     
  2. crackbrain94

    crackbrain94

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

    Thanks for your reply.

    At least is it possible to display the permission dialog on Android every time?
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @crackbrain94 Unless user grants or forever denies the permission, the dialog should show up every time.
     
  4. crackbrain94

    crackbrain94

    Joined:
    May 2, 2017
    Posts:
    12
    Unfortunately this is not happening @yasirkula.

    Is there a code I can use to display the permission dialog box for photos and videos?
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @crackbrain94 NativeGallery.RequestPermission does that, only if the permission can be asked. If operating system doesn't allow that, the dialog won't be displayed. At that point, the operating system is to blame.
     
  6. kentou1506

    kentou1506

    Joined:
    Oct 1, 2018
    Posts:
    3
    Thank you for your great asset.

    I can save and load a photo.

    But if I save a photo and delete app, after then I reinstall and try deleting the photo, it can not delete.

    Do you know how to resolve this?
     
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    App must be losing the file's ownership on uninstall. If it can be resolved, I'm unaware how unfortunately.
     
  8. danipomix

    danipomix

    Joined:
    Oct 18, 2019
    Posts:
    11
    hello Yasirkula. Thanks for all the advices you've given me so far. Sorry to keep writing to you but sometimes my app (android) keeps crashing on importing the photo - on slightly older devices (for example: a redmi M2003j15ss). I saved the path value on the sharedprefs in the NativeGalleryMediaPickerResultOperation execute, as you advised me, but
    recently I've realized that the crash is prior to this function: In fact, the message shown on the logcat when the crash occurs is "NativeGalleryMediaPickerFragment.mediaReceiver became null!!" (NativeGalleryMediaPickerFragment - line 145), so the execute of NativeGalleryMediaPickerResultOperation is not reached. So I would like to know, generally, the condition mediaReceiver == null what is usually caused by (too much memory used so the app crashes in background?)? Could you recommend me a way to minimize the occurrence of this problem?
     
    Last edited: Sep 1, 2023
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @danipomix That's worrying. I don't really know why NativeGalleryMediaPickerFragment.mediaReceiver would become null either. That if condition is there just-in-case. I'm using the legacy Fragment class instead of the AndroidX Fragment class (due to complicated manual steps of installing AndroidX). Perhaps this is a bug that is resolved in AndroidX Fragment. I'm not planning to try to migrate to AndroidX anytime soon though.
     
    danipomix likes this.
  10. Hangpt_bap

    Hangpt_bap

    Joined:
    Jul 29, 2022
    Posts:
    1
    Hi @yasirkula @crackbrain94 and everyone, I also using NativeGallery.LoadImageAtPath to load an image .heic, then I create a sprite and assign it to the Image but it still displays (?). Is there any solution?
    Code (CSharp):
    1. var tex = NativeGallery.LoadImageAtPath(path, 512, false, false);
    2. var spr = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    3. imgAvatar.sprite = spr;
     
    Last edited: Sep 20, 2023
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @Hangpt_bap Can you spot any native errors in Xcode console/logcat?
     
  12. EdChen0712

    EdChen0712

    Joined:
    May 9, 2023
    Posts:
    6
    Hello Yasirkula,

    Thank you for providing Native Gallery, it has been quite helpful to me. However, I've encountered an issue in my usage and was wondering if there's a solution.

    Under normal circumstances, Native Gallery works fine. But when I meet both of the following conditions:

    1. Installing AvatarSDK Offline version 1.9.1 in the project.
    2. Generating an iOS version of the application using Unity Cloud Build.
    I face a problem where I seemingly have the permissions, but I can't open images successfully. The error message is attached.

    The code I'm using is from the example program provided on GitHub. If needed, I can provide the testing environment.

    Thank you for your assistance
     

    Attached Files:

  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @EdChen0712 Hi, does AvatarSDK have any native iOS files with extension .mm? Or any other non-C# script in an iOS folder?
     
  14. EdChen0712

    EdChen0712

    Joined:
    May 9, 2023
    Posts:
    6
    Thank you for your response. As shown in the images, I couldn't find any relevant files. In Image 1, there are no .mm files or scripts under AvatarSDK's own Plugins in iOS. In Image 2, under the project's Plugins, there are only folders for NativeGallery and InGameDebugConsole.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      28.2 KB
      Views:
      16
    • 2.jpg
      2.jpg
      File size:
      8.3 KB
      Views:
      15
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    I don't know if it's possible that they've embedded an older version of NativeGallery in iOSInternal framework. Could you ask the author of AvatarSDK if they're using NativeGallery in their plugin and if so, if they're willing to update it to the latest version?
     
  16. EdChen0712

    EdChen0712

    Joined:
    May 9, 2023
    Posts:
    6
    Thank you for your response. After all, the version of AvatarSDK I'm using is indeed from over three years ago, so your suggestion is very valuable. I will reach out to the Avatar SDK team again for further inquiries. Thank you!
     
    yasirkula likes this.
  17. Grimmjow_Jaegerjackquez

    Grimmjow_Jaegerjackquez

    Joined:
    Dec 14, 2021
    Posts:
    16
    Hi @yasirkula , thanks for creating such a easy to use plugin. I am stuck in trying to access the Mime type of image that I pick from gallery. I am picking up png image still it gives empty string but path returns correct string.


    public void OnClickUploadImageButton()
    {
    NativeGallery.Permission per = NativeGallery.RequestPermission(NativeGallery.PermissionType.Read,NativeGallery.MediaType.Image);

    // probably should do some permissions checking here

    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
    if (path != null)
    {
    // Create Texture from selected image
    Texture2D texture = NativeGallery.LoadImageAtPath(path, 2073600, false);

    if (texture == null) return;

    ImageToBase64(path);

    //PlayerPrefs.SetString("ImagePath", imageUrl);

    NativeGallery.ImageProperties property = NativeGallery.GetImageProperties(path);

    Debug.Log("Image Path : " + path);

    Debug.Log("Mime Type" + property.mimeType);

    CropImageTaken(texture);
    }
    }, "Select an image", "image/*");
    }
     
    Last edited: Oct 31, 2023
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
  19. Grimmjow_Jaegerjackquez

    Grimmjow_Jaegerjackquez

    Joined:
    Dec 14, 2021
    Posts:
    16
    @yasirkula I just copy pasted the code of returning mime type in my class and it is working, don't know why it is not working with NativeGallery one. Thanks for the reply.
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @Grimmjow_Jaegerjackquez Could you put Debug.Logs inside the NativeGallery code I've posted to help me debug where it fails?
     
  21. Akzamutdinov

    Akzamutdinov

    Joined:
    Jan 4, 2021
    Posts:
    6
    Dear @yasirkula, first thanks a lot for providing such tools to make people life easier. I was using it for years and now I'm working on update of my app. I've added permission to manifest, I've swithced to external storage in Unity build settings, but still it does do nothing when I push button with assigned script. The task of it is simple to set player avatar from native gallery. Traget API 33. If I miss something?
     
  22. Grimmjow_Jaegerjackquez

    Grimmjow_Jaegerjackquez

    Joined:
    Dec 14, 2021
    Posts:
    16
    Hi @yasirkula , sorry for the late reply , I wrote debug for all checks and found that only debug 1 and 5 were called. Tested in editor , Unity 2022.3.11f1


    public static ImageProperties GetImageProperties( string imagePath )
    {
    if( !File.Exists( imagePath ) )
    throw new FileNotFoundException( "File not found at " + imagePath );

    #if !UNITY_EDITOR && UNITY_ANDROID
    string value = AJC.CallStatic<string>( "GetImageProperties", Context, imagePath );
    #elif !UNITY_EDITOR && UNITY_IOS
    string value = _NativeGallery_GetImageProperties( imagePath );
    #else
    string value = null;
    #endif
    Debug.Log("Debug1");
    int width = 0, height = 0;
    string mimeType = null;
    ImageOrientation orientation = ImageOrientation.Unknown;
    if( !string.IsNullOrEmpty( value ) )
    {
    Debug.Log("Debug2");
    string[] properties = value.Split( '>' );
    if( properties != null && properties.Length >= 4 )
    {
    Debug.Log("Debug3");
    if ( !int.TryParse( properties[0].Trim(), out width ) )
    width = 0;
    if( !int.TryParse( properties[1].Trim(), out height ) )
    height = 0;

    mimeType = properties[2].Trim();
    if( mimeType.Length == 0 )
    {
    Debug.Log("Debug4");
    string extension = Path.GetExtension( imagePath ).ToLowerInvariant();
    if( extension == ".png" )
    mimeType = "image/png";
    else if( extension == ".jpg" || extension == ".jpeg" )
    mimeType = "image/jpeg";
    else if( extension == ".gif" )
    mimeType = "image/gif";
    else if( extension == ".bmp" )
    mimeType = "image/bmp";
    else
    mimeType = null;
    }

    int orientationInt;
    if( int.TryParse( properties[3].Trim(), out orientationInt ) )
    orientation = (ImageOrientation) orientationInt;
    }
    }
    Debug.Log("Debug5");
    return new ImageProperties( width, height, mimeType, orientation );
    }
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
  24. Grimmjow_Jaegerjackquez

    Grimmjow_Jaegerjackquez

    Joined:
    Dec 14, 2021
    Posts:
    16
    Hmm , that explains the issue. Thanks.
     
    yasirkula likes this.
  25. lonewolf4_

    lonewolf4_

    Joined:
    May 20, 2019
    Posts:
    1
    yasir hocam merhaba,
    kodumda aracın üzerindeki stickerı kaydetme ve yükleme işlemini yapmak istiyorum ama birşeyler ters gidiyor
    işte kodların bir kaçı gerekli düzenlemeyi nasıl yapabilirim yardımcı olurmusunuz teşekürler.

    Code (CSharp):
    1. #if UNITY_ANDROID || UNITY_IOS || UNITY_WP_8 //ATTENTION: YOU NEED NATIVE GALLERY TO USE IT ON MOBILE! ////
    2.        tex = NativeGallery.LoadImageAtPath(NativeGallery.GetSavePath(Application.productName + "_Livery", fileName));
    3.  
    4.         CarMaterial[CarIndex].SetTexture("_MainTex", tex);
    5.  
    6.         SelectedLivery = 99 + slot;
    7.  
    8.         carColorManager.SelectedLivery = SelectedLivery;
    9.  
    10.         //save the slot used
    11.         PlayerPrefs.SetInt(carColorManager.CarSelected + "Livery", 99 + slot);
    12.  
    13.         StartCoroutine(UpdateMaterials());
    14.         //show text on message text : texture is loaded
    15.         if (MessageText) StartCoroutine(SavedTextAnimation("LOADED!", 1.5f));
    16. #endif
    17.  
    18.  
    19.  
    20.  
    21.  
    22. #if UNITY_ANDROID || UNITY_IOS || UNITY_WP_8 //ATTENTION: YOU NEED NATIVE GALLERY TO USE IT ON MOBILE! ////
    23.         NativeGallery.CheckPermission ();
    24.         NativeGallery.SaveImageToGallery(bytes, Application.productName + "_Livery", fileName);
    25.         yield return new WaitForSeconds (2f);
    26. #endif
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @lonewolf4_ NativeGallery.GetSavePath fonksiyonu bulunmuyor. NativeGallery.GetImageFromGallery kullanmanız lazım. Kullanıcının en son kaydettiği resme erişmek istiyorsanız, malesef resmin bir kopyasını persistentDataPath'e kopyalamak dışında bunun bir yolu bulunmuyor.
     
  27. cnsjjj

    cnsjjj

    Joined:
    Aug 13, 2022
    Posts:
    33
    Hi! @yasirkula
    Thanks for the awesome two packages!

    The version of UnityNativeGallery I use is 1.7.7, which is packaged on XCode15.0.1 and run on ios17.0.3. Both packaging and running are normal.

    But there was a small problem with the animation. When I opened the uitoolkit page and jumped to the native gallery page, the animation was normal. When I clicked Cancel again, the page was stuck and immediately jumped to the uitoolkit page. The animation was missing during the process. If I don't click cancel but close by swiping down the page, the animation works fine.

    The animation of UnityNativeFilePicker (1.3.2) freezes when opening the native page. When the close button is clicked, the page animation runs normally.

    Sorry, I can't find the cause of the problem. Is this a bug?
     
    Last edited: Nov 29, 2023 at 1:56 PM
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    @cnsjjj Hi, this is intentional. You can set all dismissViewControllerAnimated calls inside NativeGallery.mm to YES to revert it. I've encountered edge cases during the close animation (maybe when showing the dialog while it's being closed? I can't remember) which is why the animations are turned off by default.
     
    cnsjjj likes this.
  29. cnsjjj

    cnsjjj

    Joined:
    Aug 13, 2022
    Posts:
    33
    Thanks a lot! This method works for NativeGallery but not for NativeFilePicker.

    Thanks very much for your answer again!
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    I'm sorry to hear that. I'd expect the same to work for NativeFilePicker, too. Note that while displaying a dialog, presentViewController function is called, which also has its own animated property.
     
  31. cnsjjj

    cnsjjj

    Joined:
    Aug 13, 2022
    Posts:
    33
    Sorry, I still haven't been able to solve the problem.
    I changed dismissViewControllerAnimated and presentViewController to YES. The animation ran normally, but there was a problem of page jitter. The pop-up page would first animate to the top of the phone page, and then suddenly jump back to normal location.
    Thanks again!
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,799
    At first glance, I couldn't spot anything that would cause this. Could you spot possibly problematic lines in the native source code?
     
    cnsjjj likes this.
  33. cnsjjj

    cnsjjj

    Joined:
    Aug 13, 2022
    Posts:
    33
    I found it was my mistake. I didn't give the correct file type format which caused the above problem.
    All problems have been solved. I'm very sorry for causing you any trouble.
    Thanks again for your help!
     
    yasirkula likes this.