使用Android进行文件读写时,首先需要获取相应的文件路径,关于文件的路径相关内容请参考这里《彻底搞懂Android文件存储---内部存储,外部存储以及各种存储路径解惑》。
知道了相关概念,下面直接上代码。
List<string> strExternalPath = new List<string>();
void Start () {
GetFilePath("test.txt");
}
void GetFilePath(string fileName)
{
string path;
#if UNITY_ANDROID
string[] sss = new string[1] { "com" };
string[] sds = new string[1] { "Android" };
GetAndroidExternalFilesDir();
if (strExternalPath.Count <= 0)
{
path = "Not find Enable Path";
return;
}
string temp = strExternalPath[strExternalPath.Count - 1];
string[] src = temp.Split(sds, StringSplitOptions.None);
filePath = temp;
#elif UNITY_IPHONE
filePathWrite= "file://" + Application.streamingAssetsPath + "/" + fileName;
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
filePathWrite = "file:///" + Application.streamingAssetsPath + "/" + fileName;
#else
filePathWrite= String.Format("{0}/{1}", Application.streamingAssetsPath, fileName);
#endif
filePathWrite = Path.Combine(Application.persistentDataPath, fileName);
}
private void GetAndroidExternalFilesDir()
{
#if UNITY_ANDROID
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
try
{
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[]>("getExternalFilesDirs", null);
AndroidJavaObject emulated = null;
AndroidJavaObject sdCard = null;
for (int i = 0; i < externalFilesDirectories.Length; i++)
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
{
// Check which one is the emulated and which the sdCard.
bool isRemovable = environment.CallStatic<bool>("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool>("isExternalStorageEmulated", directory);
if (isEmulated)
emulated = directory;
else if (isRemovable && isEmulated == false)
sdCard = directory;
}
// Return the sdCard if available
if (sdCard != null)
strExternalPath.Add(sdCard.Call<string>("getAbsolutePath"));
else
strExternalPath.Add(emulated.Call<string>("getAbsolutePath"));
}
}
}
catch (Exception e)
{
Debug.LogWarning("Error fetching native Android external storage dir: " + e.Message);
return ;
}
}
#endif
}
如果有外部插入SD卡,name
filePath为非机身的外部存储路径,可用于读写。
Application.persistentDataPath为机身存储的内部存储路径,可用于读写。
2.写文件
public void CreateFile(string path, string info)
{
FileInfo t = new FileInfo(path);
if (!t.Exists)
{
StreamWriter sw;
//如果此文件不存在则创建
try
{
sw = t.CreateText();
//以行的形式写入信息
sw.WriteLine(info);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
SetServerIP(path, info);
}
catch (Exception e)
{
return;
}
}else
{
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine("{0}", info);
sw.Close();
sw.Dispose();
}
}
3.读文件
使用www方式从txt文件中按行读取内容。
IEnumerator doLoadByWWW(string path)
{
WWW w = new WWW(path);
yield return w;
if (w.isDone)
{
SetServerIP(path, w.text);
}
}
使用方式 StartCoroutine(doLoadByWWW("file://" + filePathWrite));
|