Hyluss

[Unity] GetExternalStorageDirectory

May 13th, 2019
519
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. private static string GetAndroidExternalFilesDir()
  2. {
  3.      using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  4.      {
  5.           using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
  6.           {
  7.                // Get all available external file directories (emulated and sdCards)
  8.                AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[]>("getExternalFilesDirs", null);
  9.                AndroidJavaObject emulated = null;
  10.                AndroidJavaObject sdCard = null;
  11.  
  12.                for (int i = 0; i < externalFilesDirectories.Length; i++)
  13.                {
  14.                     AndroidJavaObject directory = externalFilesDirectories[i];
  15.                     using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
  16.                     {
  17.                         // Check which one is the emulated and which the sdCard.
  18.                         bool isRemovable = environment.CallStatic<bool ("isExternalStorageRemovable", directory);
  19.                         bool isEmulated = environment.CallStatic<bool> ("isExternalStorageEmulated", directory);
  20.                         if (isEmulated)
  21.                             emulated = directory;
  22.                         else if (isRemovable && isEmulated == false)
  23.                             sdCard = directory;
  24.                     }
  25.                }
  26.                // Return the sdCard if available
  27.                if (sdCard != null)
  28.                     return sdCard.Call<string>("getAbsolutePath");
  29.                else
  30.                     return emulated.Call<string>("getAbsolutePath");
  31.             }
  32.       }
  33. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment