- Home /
Get a texture from persistentDataPath in Android
I need to get a texture using UnityWebRequest from Application.persistentDataPath and apply it to a plane in the scene. It works perfectly on the editor and on PC build, but on Android it cannot get the image, just says "Cannot connect to destination host". The image is there, and when i check if it exists there is no problem. Here is my code:
public void MostrarCaptura()
{
m_LocalFileName = Application.persistentDataPath + "/screenshot.png";
// ScreenCapture.CaptureScreenshot("screenshot.png");
if (File.Exists(m_LocalFileName))
{
debugText.text = "The file exists";
}
else
{
debugText.text = "No file available";
}
planeIsActive = true;
StartCoroutine(GetTextureFromPath());
}
private IEnumerator GetTextureFromPath()
{
UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(m_LocalFileName);
yield return webRequest.SendWebRequest();
while (!webRequest.isDone)
{
yield return null;
}
Debug.Log("Ends server comm");
if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.Log(webRequest.error);
debugText.text = "Error getting texture. "+webRequest.error+", \n"+m_LocalFileName;
}
else
{
downloadedTexture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
planeRend.material.mainTexture = downloadedTexture;
debugText.text = "Texture should be applied";
Debug.Log("Texture applied");
}
}
Answer by guikawano · Apr 28, 2020 at 03:52 PM
For android you need to add "file://" to the beginning of the asset path
Your answer
Follow this Question
Related Questions
Save Image to Device Local Storage - Appear in Phone Gallery 0 Answers
Assigning UV Map to model at runtime 0 Answers
Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath? 1 Answer
Save an image to PersistentDataPath then access it again 1 Answer
Fatal Exception when loading multiple image files on Android from persistent data path 0 Answers