-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryHelper.cs
46 lines (42 loc) · 1.56 KB
/
DirectoryHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Author: Anja Haumann 2018 - MIT License
// Explanation and more content at my blog: http://anja-haumann.de/unity-how-to-save-on-sd-card/
using UnityEngine;
public static class DirectoryHelper
{
public static string GetAndroidExternalFilesDir()
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and sdCards)
AndroidJavaObject[] externalFilesDirectories =
context.Call<AndroidJavaObject[]>
("getExternalFilesDirs", (object)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)
return sdCard.Call<string>("getAbsolutePath");
else
return emulated.Call<string>("getAbsolutePath");
}
}
}
}