我正在制作一个 Android 应用程序,它应该在开始时从 MySQL 数据库加载数据和从服务器加载一些图像。之后,用户将在外面,因此应用程序将离线工作。
我编写了代码以将所有数据库数据(有效)和图像从服务器保存到 ApplicationPersistenceDataPath(无效)。我已经在寻找我应该怎么做。我的代码不会引发任何错误。但是,当我尝试在应用程序上显示它时,图像是空白的。那是当我查看存储图像的文件夹时,我无法打开它们。
这是我必须保存图像的方法:
IEnumerator loadBgImage(string url, string file_name)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
print("erro");
}
else
{
string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name);
if (!File.Exists(savePath))
{
System.IO.File.WriteAllText(savePath, www.downloadHandler.text);
}
}
}
}
这是在图像中显示图像文件的代码:
string path = Application.persistentDataPath + "/" + nomeImagem;
imagem.GetComponent<SpriteRenderer>().sprite = LoadSprite(path);
这是称为的方法:
private Sprite LoadSprite(string path)
{
if (string.IsNullOrEmpty(path)) return null;
if (System.IO.File.Exists(path))
{
byte[] bytes = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(900, 900, TextureFormat.RGB24, false);
texture.filterMode = FilterMode.Trilinear;
texture.LoadImage(bytes);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.0f), 1.0f);
}
return null;
}
我真的需要先保存图像,然后离线显示。有人可以告诉我我做错了什么吗?
炎炎设计
相关分类