- Home /
Downloading image from the server crash the game in Android Device ?
The code is working perfect in editor but some time it show the high CPU usage in stats windows...that's why when build .apk and run in android device it causes crash.
Any Solution for this..
Here is my code..
void Start()
{
imgNum = 1;
StartCoroutine (Load ());
}
// Use this for initialization
IEnumerator Load ()
{
if (File.Exists (Application.persistentDataPath + ""+imgNum+".jpg"))
{
imgNum += 1;
if (imgNum > 19)
{
Panel.SetActive (false);
this.enabled = false;
}
else
{
StartCoroutine (Load ());
}
}
else
{
WWW www = new WWW("URL"+imgNum+".jpg");
yield return www;
Texture2D texture = www.texture;
byte[] bytes = texture.EncodeToJPG ();
File.WriteAllBytes (Application.persistentDataPath + ""+imgNum+".jpg", bytes);
imgNum += 1;
if (imgNum > 19)
{
Panel.SetActive (false);
this.enabled = false;
}
else
{
StartCoroutine (Load ());
}
}
}
If you will download any big large file or multiple files then it will cause a memory leak if you are using WWW class. So you can use WebClient class of c# to download the files. There is a plugin (http://u3d.as/RDf?aid=1101|43jr) to download any big file from web without memory leak. I hope this will help you. Ask me if you have any other questions.
Answer by FlyingHighUp · Oct 14, 2017 at 05:08 AM
I'm unsure, what does the adb log say?
Personally, my first suspicion is RAM. If your image is too large, (~40MB+) Android will kill your app for using too much memory. http://answers.unity3d.com/questions/547900/how-to-prevent-android-to-kill-my-unity-app-.html .
Your current code downloads 19 images in succession without disposing them, and each image is tripled because you generate a new Texture2D, then encoded it to a JPG. So you end up potentially having a whopping 57 images in memory lol
Since the data you downloaded is already in JPG format, you could just save www.bytes instead of re-encoding the image. Then right after saving it, go www.Dispose() to clear it from RAM asap (otherwise, it'll just linger).
If your images are really large, you might have to look into streaming them into files directly.
Thank u sir...this means now i have to store images a bytes and after download all images i have to call www.Dispose()..m i Right ?
Almost, more like:
WWW www = new WWW("URL"+imgNum+".jpg");
yield return www;
File.WriteAllBytes (Application.persistentDataPath + ""+imgNum+".jpg", www.bytes);
www.Dispose();
for each image. Hopefully that will solve your memory woes. Note you have to dispose each www after you're done with it.
The game stop crashing when i download image..
This time Another Error occurs when i trying to access this image it will again crash the game...and images download take much time to download...
here is my code for Fetching Image..
byte[] byteArray = File.ReadAllBytes (Application.persistentDataPath + "" + imgNum+ ".jpg");
Texture2D texture = new Texture2D (8, 8);
texture.LoadImage (byteArray);
Your answer
Follow this Question
Related Questions
few questions about using www for getting bunch of images... 0 Answers
Download and save files on iphone? 1 Answer
Loading images from web to editor window 3 Answers
www download strange...help 0 Answers
downloading file, saving on android 1 Answer