UnityNativeGallery icon indicating copy to clipboard operation
UnityNativeGallery copied to clipboard

AndroidJavaException: java.lang.NoSuchFieldError: no "Z" field "GrantPersistableUriPermission" in class "Lcom/yasirkula/unity/NativeGalleryMediaPickerFragment;" or its superclasses

Open rilla1537 opened this issue 1 year ago • 2 comments

Description of the bug

I read the documentation and used persistentDataPath, but I encountered this error. I downloaded the plugin from the Asset Store. For reference, simply retrieving the photo worked without any issues.

Reproduction steps `using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; using NativeGalleryNamespace;

public class SceneManager : MonoBehaviour { public Image imageUI; // Canvas의 Image 컴포넌트 연결 private string savedImagePath;

void Awake()
{

#if !UNITY_EDITOR && UNITY_ANDROID using( AndroidJavaClass ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeGalleryMediaPickerFragment" ) ) { ajc.SetStatic( "GrantPersistableUriPermission", true ); } #endif }

// Start is called before the first frame update
void Start()
{
    savedImagePath = PlayerPrefs.GetString("SavedImagePath", null);

    if (!string.IsNullOrEmpty(savedImagePath) && File.Exists(savedImagePath))
    {
        Debug.Log("Saved image found at: " + savedImagePath);
        StartCoroutine(LoadImageFromFile(savedImagePath));
    }
    else
    {
        PickImage();
    }
}

public void PickImage()
{
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
        if (path != null)
        {
            Debug.Log("Image URI: " + path);
            StartCoroutine(LoadImageFromURI(path));
        }
    }, "Select an image", "image/*");
}

private IEnumerator LoadImageFromURI(string uri)
{
    using (var www = new WWW(uri))
    {
        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            Texture2D texture = new Texture2D(2, 2);
            www.LoadImageIntoTexture(texture);

            // Texture2D를 파일로 저장
            string filePath = Path.Combine(Application.persistentDataPath, "savedImage.png");
            File.WriteAllBytes(filePath, texture.EncodeToPNG());

            // 저장된 경로를 PlayerPrefs에 저장
            PlayerPrefs.SetString("SavedImagePath", filePath);
            PlayerPrefs.Save();

            // Texture2D를 Sprite로 변환하여 UI에 표시
            Sprite newSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            imageUI.sprite = newSprite;

            Debug.Log("Image saved to: " + filePath);
        }
        else
        {
            Debug.LogError("Failed to load image: " + www.error);
        }
    }
}

private IEnumerator LoadImageFromFile(string filePath)
{
    byte[] imageData = File.ReadAllBytes(filePath);
    Texture2D texture = new Texture2D(2, 2);
    texture.LoadImage(imageData);

    // Texture2D를 Sprite로 변환하여 UI에 표시
    Sprite newSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    imageUI.sprite = newSprite;

    yield return null;
}

} `

Platform specs

Please provide the following info if this is a Unity 3D repository.

  • Unity version: 2022.3.37f1
  • Platform: Android
  • Device: Samsung 24 Ultra (14)
  • How did you download the plugin: Asset Store 2024-08-12

Additional info

2024-08-16 22:43:51.056 31025 31054 Error Unity AndroidJavaException: java.lang.NoSuchFieldError: no "Z" field "GrantPersistableUriPermission" in class "Lcom/yasirkula/unity/NativeGalleryMediaPickerFragment;" or its superclasses 2024-08-16 22:43:51.056 31025 31054 Error Unity java.lang.NoSuchFieldError: no "Z" field "GrantPersistableUriPermission" in class "Lcom/yasirkula/unity/NativeGalleryMediaPickerFragment;" or its superclasses 2024-08-16 22:43:51.056 31025 31054 Error Unity at com.unity3d.player.UnityPlayer.nativeRender(Native Method) 2024-08-16 22:43:51.056 31025 31054 Error Unity at com.unity3d.player.UnityPlayer.-$$Nest$mnativeRender(Unknown Source:0) 2024-08-16 22:43:51.056 31025 31054 Error Unity at com.unity3d.player.UnityPlayer$F$a.handleMessage(Unknown Source:122) 2024-08-16 22:43:51.056 31025 31054 Error Unity at android.os.Handler.dispatchMessage(Handler.java:102) 2024-08-16 22:43:51.056 31025 31054 Error Unity at android.os.Looper.loopOnce(Looper.java:230) 2024-08-16 22:43:51.056 31025 31054 Error Unity at android.os.Looper.loop(Looper.java:319) 2024-08-16 22:43:51.056 31025 31054 Error Unity at com.unity3d.player.UnityPlayer$F.run(Unknown Source:24) 2024-08-16 22:43:51.056 31025 31054 Error Unity at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 2024-08-16 22:43:51.056 31025 31054 Error Unity at UnityEngine.AndroidJNISafe.GetStaticFieldID (System.IntPtr clazz, System.String name, System.String sig) [0x00000] in <00000000000000000000000000000000>:0 2024-08-16 22:43:51.056 31025 31054 Error Unity

rilla1537 avatar Aug 16 '24 13:08 rilla1537

Asset Store version doesn't include this fix so can you update from GitHub instead? https://github.com/yasirkula/UnityNativeGallery?tab=readme-ov-file#installation

I'll update Asset Store soon.

PS. You're already copying the image to persistentDataPath so you actually don't need this fix at all. You can delete the Awake function.

yasirkula avatar Aug 16 '24 15:08 yasirkula

Also I'd recommend using UnityWebRequestTexture because that's what Unity recommends over WWW, as well.

PS. Actually use NativeGallery.LoadImageAtPath instead (it has an async variant as well) because it supports more image formats than WWW or UnityWebRequestTexture. Don't forget to set markTextureNonReadable parameter to false otherwise EncodeToPNG won't work.

yasirkula avatar Aug 16 '24 15:08 yasirkula