Search Unity

Load (video) files from SD card

Discussion in 'Android' started by Joits, Jun 19, 2017.

  1. Joits

    Joits

    Joined:
    Jul 12, 2016
    Posts:
    29
    Hi

    I'm developing an app where users are able to put their custom videos on the SD card, however i have major problems with finding the movies in the SD card folder structure.

    So far i have tried this script which i found in another thread:
    Code (CSharp):
    1.  public static string CurrentSDCardPath
    2.     {
    3.         get
    4.         {
    5.             if (m_sdCardPath == null)
    6.             {
    7.                 AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment");
    8.                 IntPtr getExternalStorageDirectoryMethod = AndroidJNI.GetStaticMethodID(jc.GetRawClass(), "getExternalStorageDirectory", "()Ljava/io/File;");
    9.                 IntPtr file = AndroidJNI.CallStaticObjectMethod(jc.GetRawClass(), getExternalStorageDirectoryMethod, new jvalue[] { });
    10.                 IntPtr getPathMethod = AndroidJNI.GetMethodID(AndroidJNI.GetObjectClass(file), "getPath", "()Ljava/lang/String;");
    11.                 IntPtr path = AndroidJNI.CallObjectMethod(file, getPathMethod, new jvalue[] { });
    12.                 m_sdCardPath = AndroidJNI.GetStringUTFChars(path);
    13.                 AndroidJNI.DeleteLocalRef(file);
    14.                 AndroidJNI.DeleteLocalRef(path);
    15.                 Debug.Log("m_sdCardPath = " + m_sdCardPath);
    16.             }
    17.             return m_sdCardPath;
    18.         }
    19.     }
    Which returns the SD card path nicely.
    Problem is finding user uploaded files. e.g. I have created a folder in the root called MyMovies and then put a example movie in that folder. Using:
    Code (CSharp):
    1. path = System.IO.Path.Combine(CurrentSDCardPath, "MyMovies");
    2.  
    3. if (Directory.Exists(path))
    4.         {
    5.             if (Directory.Exists(path + "surfers_360.wmv"))
    6.                 vidyas.Add("surfers exists!"); //vidyas is a list structure
    7.             foreach (string file in Directory.GetFiles(path))
    8.             {
    9.                 vidyas.Add(path + " file on sd card: " + file);
    10.                 vidyas.Add(file);
    11.             }
    12.         }
    13.  
    should get me the file, but it does not find any files. The path which is returned is: /storage/emulated/0/MyMovies
    Anybody got any pointers on where to go from here?

    I have tried the Application.persistentDataPath as well but with no luck.
    Write permission is "external (SD card)" and install location is "prefer external"

    Thanks.
     
    Last edited: Jun 19, 2017