Android内部ストレージと外部ストレージの取得方法
Androidを使用してファイルの読み書きを行う場合は、まず対応するファイルパスを取得する必要があります.ファイルのパスに関する内容は、ここ「Androidファイルストレージ---内部ストレージ、外部ストレージ、および各種ストレージパスの困惑を徹底的に理解する」を参照してください.
関連概念が分かりましたので、次は直接コードをつけます.
外部にSDカードが挿入されている場合は、name
file Pathは非本体の外部ストレージパスであり、読み書きに使用できます.
Application.persistentDataPathは、本体に格納されている内部ストレージパスであり、読み書きに使用できます.
2.書類を書く
3.ファイルを読む
www方式でtxtファイルから行単位でコンテンツを読み出す.
使用方法StartCoroutine(doLoadByWWWW(「file://」+filePathWrite));
関連概念が分かりましたので、次は直接コードをつけます.
List strExternalPath = new List();
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("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories = context.Call("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("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic("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("getAbsolutePath"));
else
strExternalPath.Add(emulated.Call("getAbsolutePath"));
}
}
}
catch (Exception e)
{
Debug.LogWarning("Error fetching native Android external storage dir: " + e.Message);
return ;
}
}
#endif
}
外部にSDカードが挿入されている場合は、name
file Pathは非本体の外部ストレージパスであり、読み書きに使用できます.
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(doLoadByWWWW(「file://」+filePathWrite));