Search Unity

Native Gallery for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Feb 28, 2018.

  1. tmulla

    tmulla

    Joined:
    Jan 16, 2020
    Posts:
    8
    @yasirkula , I'm encountering an issue with multiple times denying the permission, and then I go to app settings and give permission, even then images are not saving, its happening for few devices in Android, and works fine with same steps on other devices,
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  3. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    114
    I'm getting
    Code (CSharp):
    1. Assets\Plugins\NativeGallery\NativeGallery.cs(6,7): error CS0246: The type or namespace name 'NativeGalleryNamespace' could not be found (are you missing a using directive or an assembly reference?)
    2.  
    error when I try to build for Android? Any idea?
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  5. iliasM

    iliasM

    Joined:
    Dec 15, 2017
    Posts:
    15
    Hi yasirkula and many thanks for your assets! I use both of your assets (native gallery and native camera) and i try to save the video but I can't. What am i doing wrong?

    private void RecordVideo()
    {

    NativeCamera.Permission permission = NativeCamera.RecordVideo((path) =>
    {
    Debug.Log("Video path: " + path);
    if (path != null)
    {
    // Play the recorded video
    Handheld.PlayFullScreenMovie("file://" + path);
    NativeGallery.SaveVideoToGallery(System.IO.Path.Combine(Application.persistentDataPath, "video.mp4"), "Folder Name", "test.mp4");

    }
    });

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

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It must be
    NativeGallery.SaveVideoToGallery(path, "Folder Name", "test.mp4");
     
    iliasM likes this.
  7. GJBooij

    GJBooij

    Joined:
    Jun 18, 2013
    Posts:
    10
    Hello,
    I have an issue with the SaveToGallery() functionality on Android. The callback should give us a path, but instead it's always null. And I looked at the code and it makes sense.I was looking at the SaveToGalleryInternal() function:

    Code (CSharp):
    1.     private static void SaveToGalleryInternal( string path, string album, MediaType mediaType, MediaSaveCallback callback )
    2.     {
    3. #if !UNITY_EDITOR && UNITY_ANDROID
    4.         AJC.CallStatic( "SaveMedia", Context, (int) mediaType, path, album );
    5.  
    6.         File.Delete( path );
    7.  
    8.         if( callback != null )
    9.             callback( null );
    So it always returns null right, if the callback is set. So how do we know it's a success or failure?
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    GJBooij likes this.
  9. GJBooij

    GJBooij

    Joined:
    Jun 18, 2013
    Posts:
    10
    yasirkula likes this.
  10. tmulla

    tmulla

    Joined:
    Jan 16, 2020
    Posts:
    8
    It was happening in few devices because of the same file name already exists in the gallery, I changed the file name to be unique, it stared working.
     
    yasirkula likes this.
  11. Margallo

    Margallo

    Joined:
    May 1, 2016
    Posts:
    9
    Hi Yasirkula,

    I'm new here and found your plugin very useful and working perfectly, However the image size texture height and width is not what it seems to be. I upload a file to know what I'm trying to say and please how to fix this. sorry for my bad English.
     

    Attached Files:

  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Is Preserve Aspect enabled in Image's Inspector? If it is not visible, temporarily assign a sprite to it and then enable Preserve Aspect.
     
    Margallo likes this.
  13. Margallo

    Margallo

    Joined:
    May 1, 2016
    Posts:
    9
    Wow it works perfectly! Thank you very much.Do you have youtube channel so I can subcribe to you I want to learn more with your tuts. :) PS I done following you on itch.
     
    yasirkula likes this.
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I have a channel but I don't share tutorials there. So no need to subscribe but thanks for thinking about it :)
     
  15. tmulla

    tmulla

    Joined:
    Jan 16, 2020
    Posts:
    8
    Hey @yasirkula with new iOS 14 beta, storage permissions have changed, and it is causing game freeze while accepting permissions from users, do you encountered this issue or have any fix for it?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    There is an open Issue regarding iOS 14 permissions but I didn't know that it caused the game to freeze.
     
  17. tmulla

    tmulla

    Joined:
    Jan 16, 2020
    Posts:
    8
    Any updates if you're looking at fixing freeze issue?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I don't have an iOS 14 device, so I can't reproduce this issue. And the open Issue I've mentioned doesn't say anything about the game being frozen. I need some logs, some sort of useful error messages so that I can understand why this is happening.
     
  19. mostafanastary

    mostafanastary

    Joined:
    Oct 4, 2015
    Posts:
    31
    @yasirkula Hi and thank you for share the plugin.
    How can I check the file that exists in the destination in the gallery ? I don't want to save my source file over and over with {0}.
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeGallery doesn't have this functionality. The best you can do is register to MediaSaveCallback, check if returned path is not empty and points to a file (it may not on Storage Access Framework), then save it to PlayerPrefs. Afterwards, you can fetch that path from PlayerPrefs and see if File.Exists returns true for it. As you can see, this solution is not perfect and thus, we can say that this functionality isn't supported.
     
  21. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @yasirkula We're using NativeGallery v1.3.0, and were wondering if this FAQ part is true for that version? Our Android builds started to stop saving the images lately despite having permissions.
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yes, the "
    tools:node="replace"
    " solution should be version-agnostic.
     
  23. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    @yasirkula I am encountering an issue on devices with Android 10. The user gives permission the first time but the second time I try to open gallery, NativeGallery.CheckPermission() gives me Permission.Denied. It is working fine on ios.
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Does the
    tools:node="replace"/>
    solution in FAQ work? If not, does changing this line as follows work:
    AJC.CallStatic( "RequestPermission", Context, nativeCallback, readPermissionOnly, (int) Permission.ShouldAsk );
     
  25. DimaHubenkoGamepoint

    DimaHubenkoGamepoint

    Joined:
    May 16, 2018
    Posts:
    20
    Is there any chance to add a feature "Pick photo from camera" in here? Most apps allow to take avatar photo instantly
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I'm not planning to do so because taking a picture with camera requires a separate plugin (see NativeCamera) and after importing both plugins, you can simply show 2 buttons to the user: 1) pick image from the gallery or 2) take picture with the camera. As a bonus, you can make these buttons' style consistent with your app's UI style.
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi everyone! I've created an update that is mainly focused on new iOS 14 features but couldn't test it on an iOS 14 device myself since Unity doesn't support iOS 14 simulators. If you find the new features interesting and have access to iOS 14 device(s), your feedback would be much appreciated! Here's the changelog:
    • switched to PHPickerViewController on iOS 14+, adding the ability to pick multiple images/videos on iOS 14+
    • iOS 14 permissions support: added LimitedAccess to Permission enum
    • added NativeGallery.PermissionFreeMode boolean: when it is set to true, picking media from Photos will not require any permissions on iOS 11+ but the metadata of the picked media may not always be retrieved
    • CheckPermission and RequestPermission functions now take either PermissionType.Read or PermissionType.Write argument
    • When Permission is LimitedAccess, TryExtendLimitedAccessPermission can be called to prompt the user to extend their limited selection
    • Live photos can now be selected while picking images
    Download link and the use-cases I'd like to test out are available at: https://github.com/yasirkula/UnityNativeGallery/issues/136#issuecomment-691699856

    Please note that the documentation is currently out-of-date.
     
  28. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    I only have to read and I don't want to set the Write Permissions to External Sd card. but without setting write permissions to external, my permission are getting denied if I open the the gallery again.
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You can define the
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="replace"/>
    permission in your AndroidManifest and skip the "External (SD Card)" step.
     
  30. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    I have set this permission in android manifest and a change you mentioned previously in NativeGallery too but the problem still exists.
    Code (CSharp):
    1. AJC.CallStatic( "RequestPermission", Context, nativeCallback, readPermissionOnly, (int) Permission.ShouldAsk );
    Although the user gives permission first time, NativeGallery.CheckPermission() returns Denied. When the user checks in settings, the permission is allowed in settings.

    My manifest looks like this right now
    Code (CSharp):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ... >
    2.   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="replace"/> .... </manifest>
     
    Last edited: Sep 18, 2020
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I can't reproduce this issue. What is your AndroidManifest.xml file's path relative to the Unity project (e.g. Assets/SomeFolder/AndroidManifest.xml)?
     
  32. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    Solved the issue by sending readPermissionOnly=true as in NativeGallery.CheckPermission as I only needed permissions for reading media
     
    yasirkula likes this.
  33. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    @yasirkula Is the NativeGallery update for ios 14 available now? If not, do you have a timeline for that?
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  35. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    Alright. Downloaded the updated version. I am getting these errors when building in xcode

    Screenshot 2020-09-21 at 3.10.07 PM.png
     
  36. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've wrapped all iOS 14 APIs inside
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
    compiler directives. So they mustn't be compiled when iOS 14 SDK isn't used. I honestly don't know why and how you're receiving these errors.
     
  37. thetomatodev

    thetomatodev

    Joined:
    Apr 20, 2020
    Posts:
    11
    I have ios 14 sdk. Is there something i might be missing here?
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I've forgotten to mention that if you disabled automated setup, you must link the PhotosUI framework manually in Xcode. Other than that, there shouldn't be anything you are missing. I've tested the plugin on Xcode 11.6 (iOS 13) and Xcode 12.0 beta (iOS 14) and both of them compiled the app successfully. I'd recommend you to create a new Unity project, import NativeGallery and compile it with Xcode 12.0 beta.
     
  39. jimmying

    jimmying

    Joined:
    Sep 20, 2017
    Posts:
    107
    Hi @yasirkula first off thanks so much for the asset!

    I had a really old version (from around end of 2018) and that used to save the videos to "storage/emulated/0/[album name]" but after updating it changed to "storage/emulated/0/DCIM/[album name]".

    Is there any way I can go back to "../emulated/0/[album name]"?
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  41. jean21101999

    jean21101999

    Joined:
    Mar 2, 2020
    Posts:
    21
     
  42. jean21101999

    jean21101999

    Joined:
    Mar 2, 2020
    Posts:
    21
    Hi. This is my code but its not working. I have allocated all android permissions. Can you support me? Saving for both image && vide is not working and share is working for video only. To start recording video I am getting permission request.
     

    Attached Files:

  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I can't talk about NatShare since I am not the developer of that asset. First, I'd recommend you to delete {0} from your save functions, that has become obsolete long ago and might actually be the cause of the save issue.

    If the issue persists, please check logcat logs to see if any error messages pop up when you call the save functions. Also log the value of NativeGallery.RequestPermission to see what it returns.
     
  44. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    Hi there, I have a problem resizing the image. My code is-
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery(async (path) =>
    {Texture2D textureSfrompath = NativeGallery.LoadImageAtPath(path,50,false);
    And the texture becomes blurry-


    2. I also tried to not change the size NativeGallery.LoadImageAtPath(path,-1,false)
    but later when I crop the image with your ImageCropper.Instance.Show and markTextureNonReadable = false,croppedImageResizePolicy...
    width = 50; height = 50;

    I get this result-


    What import settings do I need in order to get good results? like this-
    (this pic is after NativeGallery.LoadImageAtPath(path,-1,false) took it to my PC and Windows Paint )


    Really stuck here. All I want is to load an image, crop it, and upload it to my server as 50x50 but with a good quality.
    The original file is 3024x3024. I'm pretty sure it somehow related to the process of making the texture readable
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I think the blur is an artifact of the image downscaling algorithm used by the Android OS (you can google "Bitmap.createBitmap downscale quality"). I'm downscaling the image at this line. You can try modifying the native Android library using Android Studio; I'm not planning to change the current behaviour myself. All the workarounds I've seen so far either use 3rd party image scaling libraries or very complicated functions. I'd rather keep it simple as it is.

    One alternative solution would be to use a maxSize of 512 or 1024 and then use a C# Texture downscale algorithm to resize the image to 50x50 pixels.
     
  46. jimmying

    jimmying

    Joined:
    Sep 20, 2017
    Posts:
    107
  47. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    I'm actually testing this on my Iphone not android
     
  48. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    Here is how it looks with your alternative solution-
    I changed maxsize to 512, and used
    TextureScale.Bilinear(Wall.instance.textureFromPath, 50, 50);

    By the way, if I change maxSize to -1
    and run-
    TextureScale.Bilinear(Wall.instance.textureFromPath, 50, 50);
    I get-
     
  49. Suwas93

    Suwas93

    Joined:
    Feb 8, 2018
    Posts:
    62
    I'm getting this error on Android after I enabled support for ARM64 in the project settings. Am I missing something?
     

    Attached Files:

  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @shlighter Can you try modifying these lines as follows:
    Code (CSharp):
    1. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
    2. // Resize image with UIGraphicsImageRenderer (Apple's recommended API) if possible
    3. if ([[[UIDevice currentDevice] systemVersion] compare:@"10.0" options:NSNumericSearch] != NSOrderedAscending)
    4. {
    5.     UIGraphicsImageRendererFormat *format = [image imageRendererFormat];
    6.     format.opaque = !hasAlpha;
    7.     format.scale = image.scale;
    8.    
    9.     UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:imageRect.size format:format];
    10.     image = [renderer imageWithActions:^(UIGraphicsImageRendererContext* _Nonnull myContext)
    11.     {
    12.         [image drawInRect:imageRect];
    13.     }];
    14. }
    15. else
    16. #endif
    17. {
    18.     UIGraphicsBeginImageContextWithOptions(imageRect.size, !hasAlpha, image.scale);
    19.     [image drawInRect:imageRect];
    20.     image = UIGraphicsGetImageFromCurrentImageContext();
    21.     UIGraphicsEndImageContext();
    22. }
    More resize techniques discussed here.

    @Suwas93 Looking at the Android source code, it is possible that saving images fails after 32 images (which would also explain this unresolved issue). If you add a timestamp to filename in SaveImageToGallery, is the issue resolved?