Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specified cast is not valid. #243

Closed
minhaj-inspiredsquare opened this issue Sep 26, 2023 · 1 comment
Closed

Specified cast is not valid. #243

minhaj-inspiredsquare opened this issue Sep 26, 2023 · 1 comment

Comments

@minhaj-inspiredsquare
Copy link

Hi, I'm trying to download an image from the URL but it gives me the "Specified cast is not valid" error. Here is my code please correct me on what I'm doing wrong:

public void DownloadImage(string fileUrl, string name)
    {
       
        RestClient.Get(new RequestHelper {
            Uri = fileUrl,
            // DownloadHandler = new DownloadHandlerFile(imagePath + filename),
            Method = "GET",
            DefaultContentType = true,
            EnableDebug = true,
        }).Then(res =>
        {
            Texture2D tex = ((DownloadHandlerTexture) res.Request.downloadHandler).texture;
            
            Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
            if (img != null)
            {
                img.overrideSprite = sprite;
                img.preserveAspect = true;
            }
            
        }).Catch(err => {
            Debug.Log(err.Message);
            Debug.Log("url: " + fileUrl);
        });
    }
@minhaj-inspiredsquare
Copy link
Author

Found solution:

public void DownloadImage(string fileUrl, string name)
    {
       
        RestClient.Get(new RequestHelper {
            Uri = fileUrl,
            DownloadHandler = new DownloadHandlerTexture(true),
            Method = "GET",
            DefaultContentType = true,
            EnableDebug = true,
        }).Then(res =>
        {
            Texture2D tex = ((DownloadHandlerTexture) res.Request.downloadHandler).texture;
            
            Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
            if (img != null)
            {
                img.overrideSprite = sprite;
                img.preserveAspect = true;
            }
            
        }).Catch(err => {
            Debug.Log(err.Message);
            Debug.Log("url: " + fileUrl);
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant