GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Application Crash On Android · yasirkula/UnityNativeGallery · Discussion #343

Application Crash On Android #343

Answered by yasirkula
pmeet862 asked this question in Q&A
Application Crash On Android #343
@pmeet862 pmeet862
Mar 22, 2025 · 2 comments · 5 replies
Answered by yasirkula Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

pmeet862
Mar 22, 2025

Below is my code for picking an Image from the gallery and displaying it in UI Image.

using UnityEngine;
using UnityEngine.UI;

public class ImagePicker: MonoBehaviour
{
    [SerializeField] Image _displayImg;

    private void Start()
    {
        RequestPermission();
    }

    private async void RequestPermission() {
        NativeGallery.Permission permission = await NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Read,NativeGallery.MediaType.Image);
        Debug.Log("Permission result: " + permission);
    }

    public void SelectPhotoFromGallery()
    {
        NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
        {
            if (path == null)
            {
                Debug.Log("No Image Selected");
                return;
            }

            Texture2D texture = NativeGallery.LoadImageAtPath(path, 1024);
            if (texture != null)
            {
                Sprite displaySprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100f);
                _displayImg.sprite = displaySprite;
            }
        });

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

In this, I asked for user permission at the start If I grant permission this works fine.

But If I select Don't Allow for the first time and call the SelectPhotoFromGallery() method the prompt for user permission is displayed. When I again select Don't Allow the prompt is hidden but after that, my app crashes after a short delay.

After that whenever I open the app and try to select the image by calling this SelectPhotoFromGallery() method it crashes.

Below is the Android Logcat screenshot when the app crashes.

image

You must be logged in to vote
All reactions
Answered by yasirkula Mar 22, 2025

I'm suspecting that synchronous RequestPermission inside GetImageFromGallery is causing this issue. Can you replace your code as follows and try again:

public async void SelectPhotoFromGallery()
{
	// Ask permission asynchronously
	if (await NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Read,NativeGallery.MediaType.Image) != NativeGallery.Permission.Granted)
		return;

	NativeGallery.GetImageFromGallery((path) =>
	{
		...
	});
}
View full answer

Replies: 2 comments · 5 replies

Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

yasirkula
Mar 22, 2025
Maintainer

I'm suspecting that synchronous RequestPermission inside GetImageFromGallery is causing this issue. Can you replace your code as follows and try again:

public async void SelectPhotoFromGallery()
{
	// Ask permission asynchronously
	if (await NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Read,NativeGallery.MediaType.Image) != NativeGallery.Permission.Granted)
		return;

	NativeGallery.GetImageFromGallery((path) =>
	{
		...
	});
}
You must be logged in to vote
All reactions
4 replies
@pmeet862
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

pmeet862 Mar 22, 2025
Author

@yasirkula Thanks for replying.

It's just as you expected it's the problem with synchronous RequestPermission inside the GetImageFromGallery method.

Here is the new flow that I implemented and it's working fine.

using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class ImagePicker : MonoBehaviour
{
    [SerializeField] Image _displayImg;

    private  void Start()
    {
        NativeGallery.Permission permission=  RequestPermission().Result;
        Debug.Log("Permission result at start: " + permission);
    }

    private async Task<NativeGallery.Permission> RequestPermission() {
        NativeGallery.Permission permission = await NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Read,NativeGallery.MediaType.Image);
        return permission;
    }

    public async void SelectPhotoFromGallery()
    {
        NativeGallery.Permission permission = await RequestPermission();

        Debug.Log("Permission result at selecting image: " + permission);


        if (permission != NativeGallery.Permission.Granted)
            return;

        if (NativeGallery.IsMediaPickerBusy())
            return;

       NativeGallery.GetImageFromGallery((path) =>
        {
            if (path == null)
            {
                Debug.Log("No Image Selected");
                return;
            }

            Texture2D texture = NativeGallery.LoadImageAtPath(path, 1024);
            if (texture != null)
            {
                Sprite displaySprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100f);
                _displayImg.sprite = displaySprite;
            }
        },"Pick Image");

        
    }
}
All reactions
@yasirkula
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

yasirkula Mar 22, 2025
Maintainer

Glad to hear it. In your Start function, you should await RequestPermission. Right now it doesn't wait for the permission dialog to close before returning a result, so it'll always return the default value of NativeGallery.Permission enum.

PS. To be fair, you should get rid of Start altogether because it's better UX to ask a permission only when needed.

All reactions
@pmeet862
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

pmeet862 Mar 22, 2025
Author

Ok thanks for your advice but it was just a demo project for learning.
Next, I am going to experiment around with NativeCamera and learn about that.

All reactions
@yasirkula
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

yasirkula Apr 7, 2025
Maintainer

Update: I've submitted updates to all of my native plugins, effectively removing synchronous permissions functions and enforcing the asynchronous ones.

All reactions
Answer selected by pmeet862
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

propsintime
May 6, 2025

I was able to find out with the help of ChatGTP that an IL2CCP build works instead of Mono: Here is part of the answer:
➡️ The problem was not with the authorisation itself, but with a bug/instability in the JNI (Java Native Interface) handling of the plugin, especially if you use Mono as scripting backend.

🔎 Why IL2CPP helps:

IL2CPP (Intermediate Language To C++) is much more stable for native plugins and their JNI calls.

With Mono, there are known problems with the lifecycle (especially with Unity 2023+ and Android 13/14), where plugins lose their JNI references after a restart or do not initialise correctly.

Therefore:
✅ IL2CPP: full stability.

Translated with DeepL.com (free version)

You must be logged in to vote
All reactions
1 reply
@yasirkula
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

yasirkula May 6, 2025
Maintainer

Thank you for the assist. But I believe this issue is resolved. I've replaced synchronous permission system with asynchronous permission system. If you still encounter crashes on the latest version of the plugin, do create an Issue with as much detail as possible.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
@pmeet862 @yasirkula @propsintime
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.