使用 Unity C# 从安卓内部存储访问图像和视频



我正在尝试从Android内部存储访问Whatsapp文件夹,但由于某种原因,此代码不起作用,文件路径正确,但是如果语句永远不会为真

这是用于检查文件是否执行

的代码
public static Texture2D LoadPNG()
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath))
{
Debug.Log("file exists");
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
return tex;
}

现在文件路径 ="/storage/emulated/0/Whatsapp/Media/.statuses

创建此路径的代码

public static string GetDownloadFolder()
{
string[] temp = (Application.persistentDataPath.Replace("Android", "")).Split(new string[] { "//" }, System.StringSplitOptions.None);
filePath = temp[0]+"/WhatsApp/Media/.Statuses";
return (temp[0] + "/WhatsApp/Media/.Statuses");
}

现在的问题是,在Android设备中,此.statuses文件夹具有图像和视频文件,但是此if语句从未被调用为什么我不明白甚至路径是否正确

if (File.Exists(filePath))
{
Debug.Log("file exists");
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}

您的问题是检查文件夹路径中是否存在文件。 解决方案是检查路径是否为目录并从中获取所有文件:

if (Directory.Exists(path))
{
string filenames[] = Directory.GetFiles(path);
foreach (var filename in filenames)
{
LoadPNG(filename);
}
}

最新更新