Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Load an image from file on Android

Discussion in 'Editor & General Support' started by imjusthere, Jun 6, 2017.

  1. imjusthere

    imjusthere

    Joined:
    Oct 12, 2012
    Posts:
    72
    I need to load images from the device's storage. I have some code that retrieves paths to all the images on the device. This part happens quickly. However, when I use WWW to load the images into Texture2Ds they take more than a second a piece to load. This won't work since most people have hundreds of pictures on their phones. The application also crashes a lot when there are more than a dozen or so pictures. Can someone tell me the proper way to do this or if there is something wrong with my code?

    Code (CSharp):
    1. public void LoadThumbnailImage(string path) {
    2.         WWW www = new WWW(path);
    3.         StartCoroutine(LoadThumbnailImage2(www));
    4.     }
    5.  
    6.     IEnumerator LoadThumbnailImage2(WWW www)
    7.     {
    8.         yield return www;
    9.         Texture2D tex = new Texture2D(1, 1);
    10.         www.LoadImageIntoTexture(tex);
    11.         GetComponent<RawImage>().texture = tex;
    12.     }
     
  2. scarffy

    scarffy

    Joined:
    Jan 15, 2013
    Posts:
    25
    Hi,

    I'm not sure if I could help, but I do this to load image to UI


    1. public string fileName;
    2. public Image Img;

    3. IEnumerator Start()
    4. {
    5. // Load image from mobile
    6. byte[] byteArray = File.ReadAllBytes(Application.persistentDataPath + "/" + fileName);
    7. Texture2D texture = new Texture2D(8, 8);
    8. texture.LoadImage(byteArray);
    9. Sprite s = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, 1f);
    10. Img.sprite = s;

    11. }
     
    pango likes this.
  3. samson2020

    samson2020

    Joined:
    Dec 16, 2019
    Posts:
    8
    Please could you send the code that has a path to all images
     
    markrl27 likes this.