更新日期:2021.1.13
工具:Unity2019.4.10
读取手机内存里的图片
"/storage/emulated/0" 是Android内存主目录
注意:file://+路径,总共是3个斜杠
//方式1:使用www加载
IEnumerator wwwLoadTexture(string filePath)
{
//例如 "file:///storage/emulated/0/Download/123.jpg"
WWW www = new WWW(filePath);
yield return www;
if (www.isDone && www.error == null)
{
Texture2D texture = www.texture;
//rawImage.texture = texture;
}
}
//方式2:使用UnityWebRequest加载
IEnumerator UnityWebRequestLoadPic(string dirpath)
{
//例如 "file:///storage/emulated/0/Download/123.jpg";
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(dirpath))
{
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.LogError("request error");
}
else
{
var texture = DownloadHandlerTexture.GetContent(request);
//rawImage.texture = texture;
}
}
}
读取StreamingAssets目录
public static IEnumerator load()
{
string path = string.Empty;
string line1 = string.Empty;
//Window路径
path = Application.streamingAssetsPath + "/aaa.xml"; //方法1 读取streamingAssetsPath目录
//path = "jar:file://" + Application.dataPath + "!/assets/" + "aaa.xml";//方法2和方法1一样
//PC路径
path = "file://" + Application.streamingAssetsPath + "/aaa.xml";
//或 path = Application.dataPath + "/StreamingAssets" + "/aaa.xml";
WWW wWA = new WWW(path);///WWW读取在各个平台上都可使用
yield return wWA;
line1 = wWA.text;
Debug.Log(line1);
}