Android
android.cs
public int AvailableStorageMB {
get {
var statFs = new AndroidJavaObject ("android.os.StatFs", Application.temporaryCachePath);
var availableBlocks = statFs.Call<long> ("getAvailableBlocksLong");
var blockSize = statFs.Call<long> ("getBlockSizeLong");
var freeBytes = availableBlocks * blockSize;
return freeBytes / MEGA_BYTES;
}
}
iOS
※ Android 側で指摘のように int
だと大きな値になると計算途中の値がオーバーフローする可能性があるので、大きな値を扱える型にする必要があるかもです。(コメント欄参照)
storage.mm
/*
* ストレージ空き容量の取得
*/
int storageAvailableMb_() {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error:nil];
if (dict) {
int Mb = 1024 * 1024;
float free = [[dict objectForKey: NSFileSystemFreeSize] floatValue] / Mb;
return (int)free;
}
return 0;
}
storage.cs
[DllImport("__Internal")]
private static extern int storageAvailableMb_();
public static int storageAvailableMb {
get {
if (Application.platform == RuntimePlatform.IPhonePlayer) {
return storageAvailableMb_();
}
throw new System.NotSupportedException ();
}
}