I can not pass the image to my button

Hello, I am using the Native Gallery plugins to bring an image of the android gallery up to here everything works fine but it is happening to me. that when I change the texture2d in a sprite, I do not load the new image on the button and if I remove the previous one I had. Then I leave the code to see if you can help me. thank you and sorry for my english that is not very good.

GameObject newObj;

private void PickImage( int maxSize )
    {
        NativeGallery.Permission permission = NativeGallery.GetImageFromGallery( ( path ) =>
            {
                Debug.Log( "Image path: " + path );
                if( path != null )
                {
                    // Create Texture from selected image
                    Texture2D texture = NativeGallery.LoadImageAtPath( path, maxSize );
                    if( texture == null )
                    {
                        Debug.Log( "Couldn't load texture from " + path );
                        return;
                    }

                    Destroy(newObj.GetComponent<Image> ());
                    Sprite newSprite = Sprite.Create(texture, new Rect(0,0,1024,1024), new Vector2(0.5f,0.5f));
                    newObj.GetComponent<Image>().sprite = newSprite;

                 }
            }, "Select a PNG image", "image/png", maxSize );

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

In line 20 you are destroying the Image. Don’t do that. Just remove line 20.

Hi, thank you, I’m not taking my image anymore. Now what is happening to me is that I do not put the new image I’m bringing from the gallery.

Just use some standard debugging techniques. Put in some Debug.Log() statements to learn:

  • is the code you wrote even being called?

  • is the image you get back not null?

  • what are the dimensions (width,height) of the image? Are those values reasonable?

  • what are the dimensions of the existing sprite? Are those values reasonable?

Hello, what is happening to me is that I can not debug the editor because the Native Galery plugins only run on my device. There is some way in which you can debug from the device and see the response in the editor. Thank you

You can definitely get printed information (logs) back from the device, exactly like the console window in the editor.

Look up adb logcat. It’s part of the Android SDK toolchain.

You can also buy something like Lunar Console on the appstore and have it on the device screen anywhere anytime.

Thank you very much for your help, the problem that I did not load the image was that the time to create the sprite had defined width and height 1024 and in the log I would suggest 1024 x 768.