Search Unity

  1. 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,845
    You can copy the image to Application.persistentDataPath folder using System.IO.File.Copy and store the path using PlayerPrefs.
     
    fungzilong likes this.
  2. cathyjeon

    cathyjeon

    Joined:
    Sep 5, 2021
    Posts:
    1
    hi. I want to get picture form gallery and save it at profileImage(Just like people change their profileImage in instargram). But their is an error:
    NullReferenceException: Object reference not set to an instance of an object
    FileManager+<>c__DisplayClass5_0.<PickImage>b__0 (System.String path) (at Assets/script/FileManager.cs:34)

    I don't know why this is happening. Can you help me? This is my code:


    Code (CSharp):
    1. public class FileManager : MonoBehaviour
    2. {
    3.     private string title;
    4.     private string mime;
    5.     private object callback;
    6.     public Image profileImage;
    7.  
    8.     public void BringImage()
    9.     {
    10.         PickImage(512);
    11.     }
    12.  
    13.     private void PickImage(int maxSize)
    14.     {
    15.         NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    16.         {
    17.             Debug.Log("Image path: " + path);
    18.             if (path != null)
    19.             {
    20.                 // Create Texture from selected image
    21.                 Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
    22.                 if (texture == null)
    23.                 {
    24.                     Debug.Log("Couldn't load texture from " + path);
    25.                     return;
    26.                 }
    27.                 Rect rect = new Rect(0, 0, texture.width, texture.height);
    28.                 profileImage.GetComponent<SpriteRenderer>().sprite = Sprite.Create(texture, rect, new Vector2(0.5f, 0.5f));
    29.  
    30.             }
    31.         });
    32.  
    33.         Debug.Log("Permission result: " + permission);
    34.     }
    35.  
    36. }
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    I'd say that either profileImage is null or it doesn't have a SpriteRenderer (and I must admit, it would be weird to have both Image and Sprite Renderer components together in the same object).
     
  4. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    Hi @yasirkula

    First of all I am very grateful for your plugin, it helps me a lot.
    I face the problem and hope you can help me: On android 10, when I select 1 image it works ok, but after I select more than 1 image I get this error
    [Gallery_Error] param : No Selected!
    MediaPicker.MediaPicker_Android:Gallery_Error(String)

    upload_2021-9-13_16-59-54.png
     
    Last edited: Sep 13, 2021
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Can you reproduce the issue on other devices, as well? Can I see your code?
     
  6. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    Thanks your reply,
    But i find out bug, my manifest is missing this flag: android:requestLegacyExternalStorage = true
    So it doesn't work.
    Thanks!
     
    yasirkula likes this.
  7. NeiloUnitySetup207

    NeiloUnitySetup207

    Joined:
    Jun 14, 2018
    Posts:
    3
    Hi, @yasirkula

    I built the sample code in my Android, every feature worked but loading Image.

    I can get a path of image, but nothing pop up.

    And no error messages.

    Is there any help?
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Can you assign the Texture to a RawImage? Perhaps showing the image on a cube didn't work for some unknown reasons.
     
    NeiloUnitySetup207 likes this.
  9. NeiloUnitySetup207

    NeiloUnitySetup207

    Joined:
    Jun 14, 2018
    Posts:
    3
    Thanks for your reply.

    I will try!
     
  10. NeiloUnitySetup207

    NeiloUnitySetup207

    Joined:
    Jun 14, 2018
    Posts:
    3
    It works!!

    Thank again!! :)
     
    yasirkula likes this.
  11. gopi16594

    gopi16594

    Joined:
    Mar 21, 2016
    Posts:
    1
    @yasirkula hi I tried picking image from gallery but after selecting image the path returns null. How do I fix this problem
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Can I see your code and at exactly which line the path is null?
     
  13. merpheus

    merpheus

    Joined:
    Mar 5, 2013
    Posts:
    202
    Hey @yasirkula it seems like example code snippets are only able to load a single frame of gifs. Is there a way to load the whole gif?
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    This plugin doesn't support animated gif loading, unfortunately.
     
  15. ImNewAtThis

    ImNewAtThis

    Joined:
    Feb 8, 2016
    Posts:
    10
    I'm having a problem with the permission request to add to photos freezing the app on iOS. I'm hoping that by moving the permission requests to the initial launch of the app that this problem can be avoided. I've seen on your how to section that using NativeGallery.Permission NativeGallery.CheckPermission( PermissionType permissionType ) should prompt the permission request. I'm just wondering which PermissionType and permissionType to enter to prompt the permission requests I need.
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    To save a photo to Gallery/Photos, you need Write permission. None of the permissions should freeze the app if you haven't modified the plugin's source code. You don't necessarily have to ask the permission manually since permissions are asked automatically as they are needed.
     
  17. ImNewAtThis

    ImNewAtThis

    Joined:
    Feb 8, 2016
    Posts:
    10
    I haven't modified the source code. It's an AR app, it looks for permission to add to photos when the user presses a UI button to take a screenshot to capture the AR content. I think the freeze is probably happening because the Vuforia engine is being interrupted by the permission request.
    So I'm hoping that by prompting permission requests on launch rather than when needed I can avoid interrupting the vuforia engine.
    Is it just NativeGallery.Permission NativeGallery.CheckPermission( PermissionType write ) that I need?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    You need NativeGallery.RequestPermission.
     
  19. ImNewAtThis

    ImNewAtThis

    Joined:
    Feb 8, 2016
    Posts:
    10
    Thank you. Do I need to specify the permission type?
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Yes you do. As I said before, "To save a photo to Gallery/Photos, you need Write permission".
     
  21. ImNewAtThis

    ImNewAtThis

    Joined:
    Feb 8, 2016
    Posts:
    10
    Thank you! Really appreciate the help
     
    yasirkula likes this.
  22. dfarjoun

    dfarjoun

    Joined:
    Aug 6, 2017
    Posts:
    42
    Hi,
    Do you know what's the reason for me to get always a black image as a result on IOS?
    I can see my screenshot image as UI element during playmode inside Unity, but after build, the preview and the image saved to the phone's gallery are plain black. Any idea of where might be my mistake?
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    I'd say that your screenshot function returns a black Texture2D. The example code shouldn't have this issue.
     
  24. dfarjoun

    dfarjoun

    Joined:
    Aug 6, 2017
    Posts:
    42
    Unfortunatelly I'm getting the black print screen using a copy and paste of the example code.
    I tried both with IPhone X and IPad.
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Can you reproduce it on a fresh new Unity project with default settings untouched?
     
  26. dfarjoun

    dfarjoun

    Joined:
    Aug 6, 2017
    Posts:
    42
    Hi,
    I could get an Android version of the app, copy, migrate to IOS and it worked now. Thanks for the insight!
     
    yasirkula likes this.
  27. sael-you

    sael-you

    Joined:
    Dec 5, 2020
    Posts:
    6
    Hi Yasirkula,

    i really loved your plugin while experimenting it on android but when i wanted to build an iOS version it shows this Undefined symbol errors, i followed every lead on the internet but cannot make it work, any idea on why this happens and how to fix it ?
     

    Attached Files:

    yasirkula likes this.
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
  29. NewCo-Tech

    NewCo-Tech

    Joined:
    Jan 28, 2020
    Posts:
    7
    Hi, first of all I want to say, this is a great plugin and I want to thank you for your work!

    I have a bug, when I try to save a screenshot on Andorid devices (especially on Samsung s20 and Xiami Mi 10T pro), I am getting this error.

    Error Unity AndroidJavaException: java.lang.IllegalArgumentException: MIME type application/octet-stream cannot be inserted into content://media/external/images/media; expected MIME type under image/ java.lang.IllegalArgumentException: MIME type application/octet-stream cannot be inserted into content://media/external/images/media; expected MIME type under image/*

    I am using unity editor 2021.1.23f with build target API level 31.

    Also on Huawei p30 pro and Samsung A51 works great without any error.

    Any help with this?
    Thank you in advice!
     

    Attached Files:

  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    This line in native code is supposed to return correct MIME type for images with common extensions like png or jpeg:
    String mimeType = extension.length() > 0 ? MimeTypeMap.getSingleton().getMimeTypeFromExtension( extension.toLowerCase( Locale.ENGLISH ) ) : null;


    May I see your NativeGallery.SaveImageToGallery code?
     
  31. NewCo-Tech

    NewCo-Tech

    Joined:
    Jan 28, 2020
    Posts:
    7
    Here is a snap of my code
    . Capture.JPG

    Capture1.JPG

    folderName is "New Co", with space inside..

    Thank you for your quick response!
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    string date
    doesn't end with png (date is your filename in your code). Try adding .png to the end of date.
     
  33. NewCo-Tech

    NewCo-Tech

    Joined:
    Jan 28, 2020
    Posts:
    7
    Great! Thank you, now works fine on all devices!
     
    yasirkula likes this.
  34. sam_paladin

    sam_paladin

    Joined:
    Oct 5, 2021
    Posts:
    12
    Is it possible to share directly images on whatsapp? I can do the text but image does not appear.

    There is no error on the console but Xcode one warning:

    ```
    [NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error. Extension: <EXConcreteExtension: 0x280714d80> {id = net.whatsapp.WhatsApp.ShareExtension} Items: (

    "<NSExtensionItem: 0x283f2d9c0> - userInfo: {\n NSExtensionItemAttachmentsKey = (\n \"<NSItemProvider: 0x281560d20> {types = (\\n \\\"public.image\\\"\\n)}\",\n \"<NSItemProvider: 0x281514230> {types = (\\n \\\"public.plain-text\\\"\\n)}\"\n );\n NSExtensionItemAttributedContentTextKey = {length = 318, bytes = 0x7b5c7274 66315c61 6e73695c 616e7369 ... 576f6262 6c65217d };\n \"com.apple.UIKit.NSExtensionItemUserInfoIsContentManagedKey\" = 0;\n}"```
     
    Last edited: Oct 19, 2021
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    By directly, do you mean "only" images (and not text)? If not, can you share "only" images on WhatsApp?
     
  36. sam_paladin

    sam_paladin

    Joined:
    Oct 5, 2021
    Posts:
    12
    Actually, if I just send the picture to whatsapp it works! is there any specific condition I can do for trigger this just on whatsapp? cause instagram/rest I would like to send text.
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    This is currently not supported by NativeShare so you'll need to modify the native source code (NativeShare.mm). This isn't going to be a very simple task.

    The way to do this in iOS is, for each kind of shareable content (text, url, image), you'll need to create a UIActivityItemSource implementation. Inside its itemForActivityType function, you'll be returning either the text/url/image assigned to that implementation, or nil. You'll be checking activityType (which app is selected in share sheet) to determine that. NativeShare uses a UIActivityItemSource for text and subject together: https://github.com/yasirkula/UnityN...lugins/NativeShare/iOS/NativeShare.mm#L14-L35

    In your case, you'll need 3 more UIActivityItemSource implementations: one for text when subject isn't provided, one for url and one for image. Then, you should modify their itemForActivityType functions accordingly. You can see how to pass an instance of these implementations to the share sheet here: https://github.com/yasirkula/UnityN...lugins/NativeShare/iOS/NativeShare.mm#L45-L49

    You can see an example usage of itemForActivityType that returns different strings for different apps here: https://www.albertopasca.it/whiletrue/objective-c-custom-uiactivityviewcontroller-icons-and-text/. WhatsApp doesn't seem to have a built-in UIActivityType enum, rather its raw activityType string seems to be "net.whatsapp.WhatsApp.ShareExtension".
     
  38. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, I noticed that some loaded images show a random gray line on the top.
    1.PNG

    All I am doing is getting the texture form the asset and then creating a sprite:
    Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));

    Here are the inputs I am using: NativeGallery.LoadImageAtPath(path, -1, false, false)

    I uploaded one of the images that cause this problem (as zip)

    I tried using the same image directly from assets and it does not cause this problem.
     

    Attached Files:

  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Does loading the image via Texture2D.LoadImage instead of NativeGallery.LoadImageAtPath also cause the same issue?
     
  40. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Same problem with Texture2D.LoadImage. Could it be a problem when creating the sprite?
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Try assigning the texture to a RawImage. If the issue is related to the image file, then the problem will show up on RawImage, as well.
     
  42. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Yes, same problem with RawImage. It's weird though since dragging the same image from assets work fine (no line).
    I guess it's not a big deal if it only happens to these specific images.
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    Perhaps if you set the Texture's wrapMode to Clamp it will resolve the issue.
     
    itsnottme likes this.
  44. wechat_os_Qy0_HKiEQBUSFlHzBG71eQWmw

    wechat_os_Qy0_HKiEQBUSFlHzBG71eQWmw

    Joined:
    Jun 15, 2021
    Posts:
    2
    I ran into the same problem,Do you have any new progress?
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    In this thread, author has mentioned that the plugin was working fine on a fresh new Unity project. Is it also the case for you?
     
  46. pushkinman

    pushkinman

    Joined:
    Nov 9, 2017
    Posts:
    1
    Hi, on iOS, I need to ask user for the gallery/Photos read permission but it is always allowed (the value is always true for
    NativeGallery.Permission.Granted), I want it to be false at the start of the app, how can I do that?
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    You can't do this on iOS 14 and later because the permission dialog's "Select Photos" option will freeze and then crash the app. The permission must be asked asynchronously for this to not happen but this will require significant changes and I'm not planning to do that.
     
  48. Schwupdi

    Schwupdi

    Joined:
    May 22, 2020
    Posts:
    5
    Hi, I wanted to ask how I can simply open the gallery selector on Android so that the user can then pick an Image. I then need to have this Sprite accessible, to set it on another GameObject.
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,845
    You can use GetImageFromGallery (example code available on GitHub). Then, you can use LoadImageAtPath to create a Texture from the selected image's filepath. If at any point you get an error like "Texture is not readable", simply set the function's markTextureNonReadable parameter to false.
     
  50. Schwupdi

    Schwupdi

    Joined:
    May 22, 2020
    Posts:
    5
    3 Years later and this guy still replies in under 30min, you're an absolute legend!
    Thank you so much!
     
    yasirkula likes this.