- Home /
How can I download a file (.jpg) and store it?
Hello.
I've been looking documentation and it's not clear to me. I've been trying to use "fileUtil.replacefile" with no results.
I need to download a file and store it locally so it will work without internet connection. So, the downloaded file should replace an asset.
I've been trying it for a week and I ran out of ideas, plase help! :(
Thank you so much in advance.
Answer by Paulius-Liekis · Dec 30, 2014 at 04:04 PM
You can not replace an asset by just downloading an image from the internet (well unless we're taking about Editor space, but I doubt that we are). You can download image using WWW class, and then store it anywhere on disk using something like File.WriteAllBytes(www.bytes). Later you can load the same image form local disk using same WWW class. Use WWW.texture to access the downloaded image (i.e. for putting it on a material and so on)
Thank you so much, But I have a question. This is only for windows? (.exe)... There's a way to do that for mobile devices? (.apk , .ipa)
:)
You can do the same thing for mobiles, but you have to store your own files under Application.persistenDataPath.
@edd21101, to futher what Paulius Liekis stated, you need to request the permission to be able to write on the device as well.
Worked like a charm!... Thank you so much to both of you. I had some troubles to make it in Javascript and I had to add some things to make it work... Was not that easy, but the comment was the key to the solution.
I'll leave the code I used here for anyone who needs it in the future. (Sorry about the comments in Spanish, that's my native language and it's easier for me to understand my own code.)
// I$$anonymous$$PORTO CLASES PARA USAR FIL$$anonymous$$
import System.IO;
var texLocalPoner : Texture;
var url = "http://xxxxxx.com/xxx/image01.jpg";
function Start () {
// PONGO EL DIRECTORIO PERSISTENTE EN UNA VARIABLE
var dir = Application.persistentDataPath;
//CO$$anonymous$$IENZO POR PONER LA TEXTURA QUE YA SE ENCUENTRE EN EL DISCO:
//LLA$$anonymous$$O EL DIRECTORIO CON LA CLASE WWW
var texturaLocal : WWW = new WWW("file://"+dir+"/image01.jpg");
//ESPERO A QUE CARGUE
yield texturaLocal;
//LA VUELVO TEXTURA
texLocalPoner = texturaLocal.texture;
//LA PONGO EN EL $$anonymous$$ATERIAL
renderer.material.mainTexture = texLocalPoner;
yield CargarImg();
reemplazarImg();
}
//CREA$$anonymous$$OS UNA FUNCION QUE BAJE LA I$$anonymous$$AGEN Y LA ASIGNE A UNA VARIABLE PARA USARLA LUEGO
function CargarImg(){
var www : WWW = new WWW (url);
yield www;
// AL$$anonymous$$ACENA$$anonymous$$OS LA TEXTURA DESCARGADA EN UN ARRAY DE BYTES
bytesTextura = www.bytes;
//AL$$anonymous$$ACENA$$anonymous$$OS TEXTURA DESCARGADA EN DIRECTORIO PERSISTENTE
File.WriteAllBytes(Application.persistentDataPath + "/image01.jpg",bytesTextura);
texLocalPoner = www.texture;
}
// CREO UNA FUNCION QUE REE$$anonymous$$PLAZARÁ LA TEXTURA EN EL GA$$anonymous$$EOBJECT
function reemplazarImg(){
renderer.material.mainTexture = texLocalPoner;
yield;
}
Your answer
Follow this Question
Related Questions
Making one directional virtual joystick OR smoothly transitioning between buttons 0 Answers
How to reduce scene load time on mobile? 2 Answers
Struggling with android adaptive icon. 1 Answer
Copy mp3 to cellphone 0 Answers
Does AssetBundle work on mobile? 0 Answers