- Home /
Save an image to PersistentDataPath then access it again
I want to use a button to take a screenshot and save it the the PersistentDataPath on an Android device. Then on another button press, I want to get that image and apply it as a Texture.
So far, I can successfully take the screenshot and save it (with use of a plugin) but I can't seem to access it afterwards; the texture just shows a red question mark.
I've tested with pulling existing images from my Desktop and StreamingAssets, which works fine, but it seems to have an issue doing it during runtime.
When I save the image, it goes to this file path on the Android device: /storage/emulated/0/Android/data/[my bundle identifier]/files/ScreenshotApp/MyScreenshot.jpg
The script I'm using to retrieve the image from the Android Persistent Data Path:
private IEnumerator GetImage()
{
string url = "file:///" + Application.persistentDataPath + "/MyScreenshot.jpg";
byte[] imgData;
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
imgData = www.downloadHandler.data;
if (www.error != null)
{
Debug.LogError(www.error);
console.text = www.error;
console.text += url;
}
print(url);
Texture2D tex = new Texture2D(20, 20);
tex.LoadImage(imgData);
quad.GetComponent<Renderer>().material.mainTexture = tex;
}
The pathway that the console returns, is the same as above.
I've tried changing the prefix to the pathway to "file://", "file:///", "jar:file:///" and removing the prefix altogether but nothing seems to work.
If I use "file:///" it displays the error: Unable to read data.
Using "file://" returns the error: HTTP/1.1 404 Not Found
Using "jar:file://" returns the error: HTTP/1.1 404 Not Found
If there is no prefix it shows: Unknown Error
All of them show the red question mark.
I would appreciate anyones input on this.
Thanks in advance.
Not quite sure what you mean by the texture "showing a red question mark", but that and the nature of the error message suggests that the image isn't readable. Is that right? You've tried loading the file in something else (or directly in Unity)?
So it looks like the problem is probably in how you're saving it rather than how you're loading it. You might want to share that part of the code.
If you're using a plugin for that and don't have the code you could try contacting the author. Or not using the plugin (saving an image file isn't that complicated).
bonfire is right, maybe the image is not fully saved when you load the image, ill share some code if you end up needing to re-write the pluugin yourself
IEnumerator Download()
{
WWW myWww;//change it for UnityWebRequest
yield return myWww = new WWW("The path where you first import img");
string importPath = Application.persistentDataPath+ "/Name.jpg";
System.IO.File.WriteAllBytes(importPath, www.bytes);
}
add this line behind the yield return
while(!www.isDone) { yield return new WaitForSeconds(0.2f); Debug.Log("Not finished");}
@xxmariofer Why is that necessary? They are already yielding the SendWebRequest call, doesn't that mean that we know that www is either finished or failed by the time it gets to line 7?
I am a classic guy, i continue using WWW class i have not used unitywebrequest so i am not 100% sure, and i have only used this with threads so i couldnt yield anything, with www i just create the WWW object and use the isdone property, thats why i only posted it as a comment :)
here is a code i used for basically the same task
using (WWW www = new WWW(_url))
{
while(!www.isDone)
{
await Task.Delay(TimeSpan.FromSeconds(0.2f));//here you could change it with a yield return null; since my example was
}
// assign texture
Texture2D texture = www.texture;
Image image = GetComponent<Image>();
image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
Answer by dixitabhi · Sep 11, 2020 at 11:52 AM
I think the error is because your texture is unreadable. Whenever you are generating the texture/ taking the screenshot make sure the "non readable" property of the texture is set to false. If you are using a plugin, it should have a boolean parameter to set the texture readable.