Android加载图片

xml

<Image>
<Resourses name="image" url="aaaaaa" >
<Image1>image1|http://192.168.0.98/MyItems/TankCard/Image1.jpg</Image1>
</Resourses>
</Image>

 

 

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using System.Xml;
using System.Text;

public class ndAdroiTempload : MonoBehaviour {

public Text arr1;
public Text arr2;
//资源下载保存的路径
string DownPath = "";
//读取资源的路径
string localPath = "";
//图片展示的GameObject
public GameObject rawImage;
//图片存放的路径
string filepath = null;

 

// Use this for initialization
void Start () {

// 从服务器上下载XML到本地
StartCoroutine(DownLoadToLocal("http://192.168.0.98/MyItems/TankCard/AndroidLoad.xml"));
//读取xml
StartCoroutine(ReadInAndroid());
// StartCoroutine(DownLoadToLocalIMG("http://192.168.0.98/MyItems/TankCard/Image1.jpg", "Image1"));
//StartCoroutine(DownLoadToLocal("http://192.168.0.98/MyItems/TankCard/Image1.jpg "));

}

//从服务器图片下载资源
private IEnumerator DownLoadToLocal(string url)
{
//url编码
WWW.EscapeURL(url);
//访问url
WWW www = new WWW(url);
//url解码
WWW.UnEscapeURL(url);
//根据URL获取文件的名字。
string filename = url.Substring(url.LastIndexOf('/') + 1);

//等待下载
yield return www;

 

if (www.error == null)
{
//path为你想保存文件的路径。
FileStream fs = File.Create(Application.persistentDataPath + "/" + filename);
DownPath = Application.persistentDataPath + "/" + filename;
//把下载的数据写到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();

Debug.Log(filename + "下载完成");

}
else
{
Debug.Log(www.error);
}
}

//解析Xml
//安卓解析XML
//用于读取文件的协程函数
IEnumerator ReadInAndroid()
{
localPath = Application.streamingAssetsPath + "/AndroidLoad.xml";
WWW www = new WWW(localPath);
while (!www.isDone)
{
Debug.Log("Getting GetXML");
yield return www;
ParseXml(www);

}
}
//解析
public void ParseXml(WWW www)
{
List<XmlNode> xmlNodeList = new List<XmlNode>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.text);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Image").ChildNodes;
//遍历每一个节点,拿节点的属性以及节点的内容
foreach (XmlElement xe in nodeList)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
//分割为字符串数组(Split)
string newstr = x1.InnerText;
//Debug.Log("newstr+"+newstr);
string[] arr = newstr.Split('|');

arr1.text = "地址是:" + arr[1];
arr2.text = "名字是:" + arr[0];
if (File.Exists(Application.persistentDataPath + "/" + x1.Name + ".jpg"))
{
rawImage.GetComponent<RawImage>().texture = Instantiate(LoadIMG(x1.Name + ".jpg")) as Texture;
}
else
{
//从服务器上下载图片
//XML文件中的地址不能多出空格++++++++++++
StartCoroutine(DownLoadToLocalIMG(arr[1],x1.Name));
// StartCoroutine(DownLoadToLocalIMG("http://192.168.0.98/MyItems/TankCard/Image1.jpg"));

}
}

}

}
//从服务器下载IMG资源
private IEnumerator DownLoadToLocalIMG(string url, string strname)
{


//url编码
WWW.EscapeURL(url);
//访问url
WWW www = new WWW(url);
//url解码
WWW.UnEscapeURL(url);
//根据URL获取文件的名字。
string filename = url.Substring(url.LastIndexOf('/') + 1);

//等待下载
yield return www;

//Instantiate(www.assetBundle.mainAsset);

if (www.error == null)
{
Debug.Log(www);
//path为你想保存文件的路径。
FileStream fs = File.Create(Application.persistentDataPath + "/" + filename);
DownPath = Application.persistentDataPath + "/" + filename;
//把下载的数据写到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();

Debug.Log(filename + "下载完成");

//加载img
rawImage.GetComponent<RawImage>().texture = LoadIMG(strname + ".jpg");
}
else
{
Debug.Log(www.error);
}
}

//从磁盘上读取文件内容 Texture2D
Texture2D LoadIMG(string ImgName)
{
//实例化一个文件流
FileStream fs = File.Open(Application.persistentDataPath + "/" + ImgName, FileMode.Open);
//把文件读取到字节数组
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
//实例化一个内存流--->把从文件流中读取的内容[字节数组]放到内存流中去
MemoryStream ms = new MemoryStream(data);
//创建Texture
int width = 85;
int height = 85;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(data);
return texture;
}

}

posted @ 2016-06-24 15:33  Fei非非  阅读(208)  评论(0编辑  收藏  举报