博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android加载图片
阅读量:4496 次
发布时间:2019-06-08

本文共 3841 字,大约阅读时间需要 12 分钟。

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;
}

}

转载于:https://www.cnblogs.com/ZeroMurder/p/5614343.html

你可能感兴趣的文章
Delphi的命令行编译命令
查看>>
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》--微软中国.NET Micro Framework项目组工程师所作之序...
查看>>
php服务端搜索,功能改进
查看>>
unity, 在surface shader中访问顶点色
查看>>
Spring声明式事务配置
查看>>
并查集的实现
查看>>
Leetcode 350. Intersection of Two Arrays II
查看>>
EditPlus VC2010 and 2008 C/C++配置
查看>>
Practical Lessons from Predicting Clicks on Ads at Facebook
查看>>
JFrame面板
查看>>
Android自动化压力测试之Monkey Test 异常解读(五)
查看>>
Compressing Convolutional Neural Networks in the Frequency Domain 论文笔记
查看>>
设计模式:单例和多例
查看>>
Myslq 之修改数据库
查看>>
maven工程转为web工程时没有add web project capabilities选项的解决办法
查看>>
[BZOJ1192][HNOI2006]鬼谷子的钱袋
查看>>
Linux系统基础优化
查看>>
小程序开发快速入门教程(附源码)
查看>>