GeezerVison is showing github.com — Application Crash On Android · yasirkula/UnityNativeGallery · Discussion #343
Application Crash On Android
#343
Mar 22, 2025
·
2 comments
·
5 replies
Discussion options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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.

|
You must be logged in to vote
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) =>
{
...
});
}
Replies:
2 comments
·
5 replies
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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
4 replies

Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
@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");
}
}
|
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.
|
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.
|
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
Update: I've submitted updates to all of my native plugins, effectively removing synchronous permissions functions and enforcing the asynchronous ones.
|
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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
1 reply
Comment options
-
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
|