Search Unity

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,879
    I have 2 sections in the native code that deliberately attempt to avoid this issue:

    - https://github.com/yasirkula/UnityN.../NativeGallery/iOS/NativeGallery.mm#L756-L778
    - https://github.com/yasirkula/UnityN...ativeGallery/iOS/NativeGallery.mm#L1030-L1048

    I don't think the Code=7 log is related to this problem and the native code would log error messages if something were to go wrong in the above sections. I unfortunately don't know how this issue is occurring, let alone on both of your devices. The logs don't give me anything useful to debug the issue.
     
    stilkin likes this.
  2. ZiadJ

    ZiadJ

    Joined:
    Sep 3, 2011
    Posts:
    62
    First I'd like to say thank you for making this plugin open source. From what I understood, the plugin apparently only allows to take pictures by pressing on a button but is there a way to do so without user intervention?
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeGallery isn't a screen capture plugin, it is a plugin for saving media to Gallery or fetching media from Gallery. You can save media to Gallery without user intervention but they may need to accept gallery permission first. You can't pick a media from Gallery without user intervention, they must pick the media manually.
     
    protopop, stilkin and ZiadJ like this.
  4. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    Hello,

    Does this work in WebGL? I want to use on my web app and I need to take pictures then allow user to either save picture to gallery or share via Social media.

    Thanks,
    jrDev
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It doesn't. Only Android & iOS are supported.
     
  6. artmatv

    artmatv

    Joined:
    Apr 20, 2018
    Posts:
    2
    Hello! I got this message from Google a few weeks ago, is there any solution for this? I'm a little confused about what I need to do.

    We've detected that your app contains the requestLegacyExternalStorage flag in the manifest file of 1 or more of your app bundles or APKs.

    Developers with apps on devices running Android 11+ must use Scoped Storage to give users better access control over their device storage. To release your app on Android 11 or newer after May 5th, you must either:

    1. Update your app to use more privacy friendly best practices, such as the Storage Access Framework or Media Store API

    2. Update your app to declare the All files access (MANAGE_EXTERNAL_STORAGE) permission in the manifest file, and complete the All files access permission declaration in Play Console from May 5th

    3. Remove the All files access permission from your app entirely
    For apps targeting Android 11, the requestLegacyExternalStorage flag will be ignored. You must use the All files access permission to retain broad access.

    Apps requesting access to the All files access permission without a permitted use will be removed from Google Play, and you won't be able to publish updates.
     
    protopop likes this.
  7. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  8. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    178
  9. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    At the moment, I'm happy with supporting only Android & iOS but thank you for the suggestion.
     
    protopop and CrandellWS like this.
  10. FenrirGameStudio

    FenrirGameStudio

    Joined:
    Dec 4, 2017
    Posts:
    7
    Hey @yasirkula
    Thank you so much for this wonderful solution, saved me a lot of headaches!

    While the solution works perfectly, we are now trying to list the image files by name to our users, which works perfect on Android, but on iOS the image name is "pickedMedia1"

    Is there a way in which to get the correct file name on iOS? : )

    Thank you in advance!
    And keep up the great work mate!
     
  11. Jo_Da

    Jo_Da

    Joined:
    May 12, 2021
    Posts:
    1
    hi! @yasirkula
    It was updated to version 1.6.3
    Unity : 2018.4.11f

    Code (CSharp):
    1. saveDir = AJC.CallStatic<string>( "GetMediaPath", Application.productName );
    'GetMediaPath' Error occurred.
    nativegallery_error.PNG

    What should I do?
    It is difficult to get information.:(
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @FenrirGameStudio It requires modifying parts of the native code that use
    pickedMediaSavePath
    . It won't always work because in some cases, iOS returns just a UIImage so we don't have a file path to work with in those cases.

    @Jo_Da GetSavePath has been removed a long time ago. You can see this thread for a discussion about it: https://github.com/yasirkula/UnityNativeGallery/issues/78#issuecomment-541333226. You can take a look at SaveImageToGallery's callback function and see if it works for you.
     
  13. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    After updating to xcode last version i get this errors from nativegallery.mm


    Incompatible pointer types assigning to 'NS_REFINED_FOR_SWIFT id<PHPickerViewControllerDelegate>' from 'Class'
    Incompatible pointer types assigning to 'NS_REFINED_FOR_SWIFT id<PHPickerViewControllerDelegate>' from 'Class'
    Incompatible pointer types assigning to 'NS_REFINED_FOR_SWIFT id<PHPickerViewControllerDelegate>' from 'Class'


    Any solution to fix this?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is the plugin up-to-date? Can you delete it and reimport it?
     
  15. FenrirGameStudio

    FenrirGameStudio

    Joined:
    Dec 4, 2017
    Posts:
    7
    Thanks for letting me know! : D
    I'll see what I can figure out, if I find a solution I'll get back to you : )
    Cheers!
     
    yasirkula likes this.
  16. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Update, after remporting the asset from the store on the mac device now the error is gone BUT i can not share (with nat

    Update, after rebooting and remporting again the asset from the store on the mac device now the error is gone...
    BUT i can not share (with nativeshare asset) on whatsapp, it work fine on teegram but on whatsapp just share the text and not the image, why?

    This is the code:


    Code (CSharp):
    1.   public void TakeScreenshot()
    2.     {
    3.         StartCoroutine(CaptureScreen());
    4.     }
    5.     public IEnumerator CaptureScreen()
    6.     {
    7.         yield return null;
    8.         // Take screenshot
    9. string        namefileRecorded = "BV_Screenshot.jpg";
    10.  
    11.   string      fullPathRecorded = Path.Combine(Application.persistentDataPath, namefileRecorded);
    12.  
    13.  
    14.  
    15.         if (File.Exists(fullPathRecorded))
    16.         {
    17.             Debug.Log("exist");
    18.             File.Delete(fullPathRecorded);
    19.             yield return new WaitForEndOfFrame();
    20.         }
    21.  
    22.  
    23.  
    24.                 ScreenCapture.CaptureScreenshot(namefileRecorded);
    25.    
    26.  
    27.  
    28.             Debug.Log("salvato screenshot in" + fullPathRecorded);
    29.      
    30.  
    31.  
    32.         //  yield return new WaitForSeconds(1f);
    33.  
    34.  
    35.  
    36.      string   finalPath = Path.Combine(Application.persistentDataPath, "asd");
    37.  
    38.         string pathcartella = finalPath;
    39.  
    40.         if (!Directory.Exists(pathcartella))
    41.         {
    42.             Directory.CreateDirectory(pathcartella);
    43.             Debug.Log(pathcartella);
    44.         }
    45.  
    46.         finalPath = Path.Combine(finalPath, namefileRecorded);
    47.  
    48.  
    49.         while (!File.Exists(fullPathRecorded))
    50.             yield return null;
    51.  
    52.  
    53.  
    54.  
    55.         if (File.Exists(finalPath))
    56.         {
    57.             Debug.Log("exist2");
    58.             File.Delete(finalPath);
    59.             yield return new WaitForEndOfFrame();
    60.         }
    61.  
    62.  
    63.         Debug.Log("startmove");
    64.  
    65.         File.Move(fullPathRecorded, finalPath);
    66.         Debug.Log("endmove");
    67.  
    68.  
    69.  
    70.         // Show UI after we're done
    71.         yield return new WaitForSeconds(0.1f);
    72.  
    73.         new NativeShare().AddFile(finalPath)
    74.        .SetText("Show this amazing content!")
    75.        .SetCallback((result, shareTarget) => ShareVideoCallback(result))
    76.        .Share();
    77.     }
    78.     public void ShareVideoCallback(NativeShare.ShareResult result)
    79.     {
    80.  
    81.  
    82.     }
    Please help me I am stuck for a while...

    It work ok on telegram but on whatsapp get this:


    2021-05-14 12:20:24.378068+0200 Mira[795:160173] [core] SLRemoteComposeViewController: (this may be harmless) viewServiceDidTerminateWithError: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
    2021-05-14 12:20:24.868922+0200 Mira[795:160173] Shared to net.whatsapp.WhatsApp.ShareExtension with result: 1
    2021-05-14 12:20:24.870622+0200 Mira[795:160347] [ShareSheet] connection invalidated
    2021-05-14 12:20:43.674870+0200 Mira[795:160173] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
     
    Last edited: May 14, 2021
  17. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, is it possible to check the file size in MB (or set max size) of the image that I get from GetImageFromGallery?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can't set a file size limit with NativeGallery, unfortunately. After the image is picked, you can check its file size via
    new FileInfo(imagePath).Length
    .
     
    itsnottme likes this.
  19. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    Hi there, your assets is great I already gave you 5 stars on assets store, I am having problem saving the image inside the unity game that I got from android gallery using your example code, actually i want the image to be saved inside the image gallery of game and when user closes the game or scene transitions performed those saved images remain saved inside the game. PLEASE HELPPPPPP..............!!!!!!!!!!!! It will be very helpful if you can write some example funtions for me. Thanks.
     
  20. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    please help.
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    The only way to achieve this is to copy the returned image file to
    Application.persistentDataPath + "/SavedImage.png"
    using File.Copy and then reload it from that path via NativeGallery.LoadImageAtPath when the image is needed. But you need to make sure that the file exists at that path via File.Exists.
     
  22. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    Can you help me with the code I am beginner to unity game dev. please can you write a functions for saving image using Application.persistentDataPath + "/SavedImage.png" , I shall be very very thankful to you, this is part of my exam so please help. PLEASE.
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Sure. To save the image at path imagePath to that path:
    System.IO.File.Copy(imagePath, Application.persistentDataPath + "/SavedImage.png", true);
     
    Last edited: May 26, 2021
  24. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    Thank you so much, it really helped me out, one last question please how can I load that copied image using NativeGallery.LoadImageAtPath ???
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Code (CSharp):
    1. Texture2D texture = null;
    2. if(File.Exists(Application.persistentDataPath + "/SavedImage.png"))
    3.     texture = NativeGallery.LoadImageAtPath(Application.persistentDataPath + "/SavedImage.png", 1024);
     
  26. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    I want to know how to check if the saved image file at persistentDataPath Exists or not?? I am using C# developing a game I tried to use IF statment like this but it doesn't work.

    if(File.Exists(Application.persistentDataPath + "/MyAlreadySavedImageFile.png") == "MyAlreadySavedImageFile.png")

    Need to use IF statement because I have to check a series of images to compare.

    Any Suggestions??
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    if(File.Exists(Application.persistentDataPath + "/MyAlreadySavedImageFile.png"))
     
  28. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    Thanks alot you are champion
     
    yasirkula likes this.
  29. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, how do I handle gifs if there is one at the path?
    I assume: texture = NativeGallery.LoadImageAtPath(path, -1, false, false); wouldn't work because gif can't be loaded into a texture.

    Also, I have one more question if you don't mind.
    Does the downscale use Bilinear or Point?

    Thanks.
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    LoadImageAtPath was able to load all OS-supported images in my tests. Gifs won't be animated, though. Downscaling is handled by OS' native implementation. I don't know which algorithm it uses but the end results have always looked smooth to me, so I don't think Point downscaling is used.
     
    itsnottme likes this.
  31. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    [The minSDk version can not be specified in the AndroidManifest.xml file. You have to remove it. I am getting this error when I build the game using unity. I am using firebase and native gallery plugin. I have two android manifest files in my project screen shoots are attached. PLEASE HELP ME.][1]

    [link of the error i am facing][1]

    [manifest file of Firebase][2]

    [manifest file of assets>plugins>Android][3]


    [1]: https://i.stack.imgur.com/hqOoP.png
    [2]: https://i.stack.imgur.com/9i2MO.png
    [3]: https://i.stack.imgur.com/ab0BY.png


    PLEASE HELP
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Does deleting NativeGallery resolve the error?
     
  33. Leonard_InitializeGame

    Leonard_InitializeGame

    Joined:
    Jun 1, 2020
    Posts:
    5
    Hi, I meet some problem
    The exif data from image captured by Native Gallery is missing GPS related data in iOS

    I don't know it is the iOS limitation or Native Gallery...
    Android exif.png IOS exif.png
     
  34. Nabeel-Akhtar

    Nabeel-Akhtar

    Joined:
    Mar 30, 2015
    Posts:
    6
    Hi,
    I am having this error when trying to build my project for ios. Anybody have an idea what is wrong over there. The same project works fine for android.

    "
    Incompatible pointer types assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from 'Class'
    "

    Unity version: 2019.3.3f1
    Xcode: 12.5

    Error image link: https://ibb.co/P9Wsp2F

     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Leonard_InitializeGame GPS data is stripped by iOS. It may be retrievable when full Photos permission is granted but it isn't a supported use-case in NativeGallery for unrelated technical reasons.

    @Nabeel-Akhtar You should delete the plugin completely and reimport the latest version.
     
  36. Nabeel-Akhtar

    Nabeel-Akhtar

    Joined:
    Mar 30, 2015
    Posts:
    6
    @yasirkula Thank you I will be doing that and will be updating for sure.
     
  37. Ahmadounity

    Ahmadounity

    Joined:
    Mar 21, 2019
    Posts:
    9
    Also tried that but same error. What should i do?
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You need to ask this question on Android sub-forum. No need to mention NativeGallery there since it isn't the cause of the issue, as you said. I'd recommend you to google the error first, though:
    unity minsdk version can not be specified in the AndroidManifest.xml
     
  39. Leonard_InitializeGame

    Leonard_InitializeGame

    Joined:
    Jun 1, 2020
    Posts:
    5
    Thank you for your reply
     
  40. dNazarik

    dNazarik

    Joined:
    Feb 6, 2016
    Posts:
    7
    Hey @yasirkula

    I have an app freeze on iOS 14.6 when choosing "Select photos".
    I found that line 223 won't be called in this case:
    image.png

    "Permission free mode" is not an option for me.

    Could you help, please?
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    With the current synchronous permission system, it is impossible to resolve the freeze when "Select photos" is clicked. You need to either don't modify permission free mode (why would you do this anyways) or heavily modify both NativeGallery.mm and NativeGallery.cs so that permissions are asked asynchronously. It'll be a tedious task so I can't help with that :/
     
    dNazarik likes this.
  42. unity03_unity133

    unity03_unity133

    Joined:
    May 26, 2021
    Posts:
    3

    I have this exact same problem. Removing the asset and reinstalling it doesn't fix it. Can I just comment that line or there is some other way to fix it?
    Unity Version: 2020.1.4f.
    XCode: 12.5
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  44. unity03_unity133

    unity03_unity133

    Joined:
    May 26, 2021
    Posts:
    3
    thanks, it temporary solved the problem (if I rebuild I need to remember to change this line)
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can change the line inside Unity. There's NativeGallery.mm there, too.
     
  46. OrenD

    OrenD

    Joined:
    Jul 14, 2020
    Posts:
    8
    Hi Yasirkula,

    I would like to ask permission to the user to view the users gallery in order to pick a picture. I played with the objective C nativegallery.mm because i see it automaticaly gives permission if > ios 11. I want all iOS's to ask for permission. So after I changed the setting from 1 to 2 it did nothing. I notice in debug.log the permission status is indeed changed to ask for permission. However no message ever pops up asking for it. So i'm unsure how to proceed. Hopefully I don't have to write a message plugin, but I was considering that. Do you have any thoughts?
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  48. OrenD

    OrenD

    Joined:
    Jul 14, 2020
    Posts:
    8
    Hi Yasirkulla,
    That is exactly the bug I ran into. And it DID freeze indefinitely. I'm wondering how I can get it to pop up a message asking for permission instead of freezing. Any ideas?

    "iOS 14+'s "Select Photos" option in permission dialog doesn't support it"... what does this mean? Are you saying that when the permission changes from "granted" to "ask for permission" that iOS 14+ doesn't know how to ask?
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Select Photos doesn't support synchronously selecting photos, it freezes indefinitely. Are you attempting to show a permission popup and just ignore its result? You want to ask the permission just for the sake of showing the permission dialog and don't care which option the user has picked? Why?
     
  50. OrenD

    OrenD

    Joined:
    Jul 14, 2020
    Posts:
    8
    To be clear, here is what I want:
    When the user clicks on the "add a photo" button, a dialogue pops up asking for permission to view the photo gallery. If the user selects "yes", then the gallery pops up and we do the thing. In the future, no dialogue pops up for this user agian. If the user selects "no", the gallery does not pop up, we don't do the thing. In the future, a diologue pops up saying you need to give access in settings for this app to view the gallery.

    What do you mean "select photos doesn't support synchronously selecting photos"? Do you mean that it supports asynchronously selecting photos? And as such you cannot ask for and grant permissions?

    I understand that the method GetImageFromGallery is asynchronous. Before that I am using the method RequestPermission and it doesn't do the popup dialogue.

    Is it now clear what I'm attempting to do? (btw thanks for this awesome free plugin, I want to preface that before we go any further)