Search Unity

Simple File Browser [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Nov 19, 2016.

  1. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Hi bro, i am developing an application to read videos of a specific route, I read it from the internal memory but I also want to read them from the external storage.

    Are there any way to get only the android external path from your script??
     
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  3. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Hi, I have this error when executing:

    Exception: JNI: Init'd AndroidJavaClass with null ptr!
    UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:622)
    UnityEngine.AndroidJavaObject.get_JavaLangClass () (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:600)
    UnityEngine.AndroidJavaObject.FindClass (System.String name) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:591)
    UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:611)
    UnityEngine.AndroidJavaClass..ctor (System.String className) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJavaBindings.gen.cs:92)
    SdCard.GetAndroidExternalFilesDir () (at Assets/SdCard.cs:7)
    SdCard.Start () (at Assets/SdCard.cs:40)

    executed the method GetAndroidExternalFilesDir on method Start.
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    What is SdCard.cs like? Please provide the full code if possible.
     
  5. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class SdCard : MonoBehaviour {
    5.     private static AndroidJavaClass my_androidJavaClass = null;
    6.     private static AndroidJavaClass AJC
    7.     {
    8.         get
    9.         {
    10.             if (my_androidJavaClass == null)
    11.             {
    12.                 my_androidJavaClass = new AndroidJavaClass("com.yasirkula.unity.FileBrowser");
    13.             }
    14.             return my_androidJavaClass;
    15.         }
    16.     }
    17.  
    18.     private static AndroidJavaObject my_context = null;
    19.     private string DEFAULT_PATH;
    20.     private string drivesList;
    21.  
    22.     private static AndroidJavaObject Context
    23.     {
    24.         get
    25.         {
    26.             if (my_context == null)
    27.             {
    28.                 using (AndroidJavaObject unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    29.                 {
    30.                     my_context = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    31.                 }
    32.             }
    33.  
    34.             return my_context;
    35.         }
    36.     }
    37.  
    38.     public Text text;
    39.  
    40.     public void InitializeQuickLinks()
    41.     {
    42.         string drivesList = AJC.CallStatic<string>("GetExternalDrives");
    43.         if (drivesList != null && drivesList.Length > 0)
    44.         {
    45.             bool defaultPathIntialized = false;
    46.             int driveIndex = 1;
    47.             string[] drives = drivesList.Split(':');
    48.             for (int i = 0; i < drivesList.Length; i++)
    49.             {
    50.                 try
    51.                 {
    52.                     string driveName;
    53.                     if (!defaultPathIntialized)
    54.                     {
    55.                         DEFAULT_PATH = drives;
    56.                         defaultPathIntialized = true;
    57.  
    58.                         driveName = "Memoria interna";
    59.                     }
    60.                     else
    61.                     {
    62.                         if (driveIndex == 1)
    63.                         {
    64.                             driveName = "Memoria externa";
    65.                         }
    66.                         else
    67.                         {
    68.                             driveName = "Memoria externa" + driveIndex;
    69.                             driveIndex++;
    70.                         }
    71.                     }
    72.                 }
    73.                 catch { }
    74.             }
    75.         }
    76.     }
    77.  
    78.     void Start()
    79.     {
    80.         InitializeQuickLinks();
    81.         text.text = drivesList;
    82.     }
    83. }

    just is a copy/paste
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  7. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33

    Hi bro, i made a new class to test your plugin and it worked.
    I can already get the type of memory that in this case is the external memory, but how do I get a specific path, for example / storage / 2604-E07E (number of sd) / Videos / Action? Sorry, i am very noob

    Screenshot in Unity Android APP:
    https://www.dropbox.com/s/3cbip4r9exar7bu/Screenshot_20180719-134808.png?dl=0

    /storage/2604-E07E(number of sd)/Videos/Action
    https://www.dropbox.com/s/n0srqe3oikosom2/Screenshot_20180719-134710.png?dl=0


     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  9. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Thank bro it work! Id dont use the Path.Combine method.
     
    yasirkula likes this.
  10. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Nice asset! Your page mentions that it works on Windows + Android, but doesn't on UWP. Does it work on iOS then, for example for the StreamingAssets folder? What's the reason that this doesn't work on UWP?
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    On iOS, file browser shows Application.persistentDataPath by default. Most of the parent directories can not be browsed due to SecurityException's. I haven't checked whether or not you can navigate from persistentDataPath to streamingAssetsPath, but you can easily add streamingAssetsPath as a quick link to the file browser.

    On UWP, you don't have permission to access most of the directories. In a way, it is similar to iOS, but this behaviour is very likely not what end-users would expect on a Windows app.
     
  12. coatol5

    coatol5

    Joined:
    Oct 16, 2015
    Posts:
    2
    Hello,

    First off great asset! I'm find it very easy to use and integrate with my project. I am running into a problem with my build though...

    When testing in the Unity editor, your file browser system works perfectly. But when I run it in a build, the browser does not seem to appear. I have a function that, upon button press, will call the ShowLoadDialogueCoroutine function which calls FileBrowser.WaitForLoadDialog - exactly following the example you included on the project's github documentation. Again, when running in the Editor, this opens up the file browser and it works no problem, but when running in a build, nothing comes up.

    I just have a regular Windows PC build, x86_64 architecture. I'm using Unity 2018.1.8f1 and I'm running a Windows 10 desktop.

    Is there some setting I am missing that I'm supposed to enable to get this to work in a build?

    Thank you.

    EDIT:

    I seemed to have found the solution. I was trying to use SimpleFileBrowser in a different scene than my opening scene. I created an instance of the SimpleFileBrowser in my opening scene, and disabled it, and it seems to work now in all subsequent scenes.
     
    Last edited: Aug 6, 2018
  13. Guile76

    Guile76

    Joined:
    Jun 7, 2017
    Posts:
    2
    Hello,
    Thank you for your asset, I really like it :)
    Isn't it possible to use it in a WebGL build ? I'm not well aware of the limits with this kind of build, so could you shortly explain why it shouldn't ?
    :)
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @coatol5 That's weird. Unless contents of the Resources directory get stripped from your build, SimpleFileBrowser prefab inside the Resources directory should be instantiated automatically the first time you access the file browser. Are there any error messages in the console when you don't put a deactivated instance in your opening scene (you can use IngameDebugConsole to debug your game)?

    @Guile76 WebGL apps don't have access to the local file system. The following plugin uses a native Javascript library to display native dialogs on WebGL: https://github.com/gkngkc/UnityStandaloneFileBrowser
     
  15. Vasilis-Mbakalis

    Vasilis-Mbakalis

    Joined:
    May 23, 2017
    Posts:
    16
    Hello, great asset, very helpful!!
    Is there any way to choose more than one files when file browser is open?
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Currently, this is not possible.
     
  17. Guile76

    Guile76

    Joined:
    Jun 7, 2017
    Posts:
    2
    Thanks for the answer and for the link. :)
     
  18. vishesh-l

    vishesh-l

    Joined:
    Feb 14, 2010
    Posts:
    27
    hi so i want to use your file browser for a VR app im making, but i have a problem.
    for my VR app i need to have physics colliders on all menu items. including the file browser buttons.
    i added colliders to back,forward,up,ok and cancel buttons because they were already there on the ui.
    how do i add colliders to folder buttons in the quick links and files area?

    basically i need to add colliders to all buttons which the user needs to interact with.
    can you please tell me where you are creating the buttons so that i can add colliders to the buttons when creating them?
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Quick links are created here and files/folders are created here. They use the prefabs located inside the Prefabs folder. Please keep in mind that they have a flexible width that change when the file browser is resized.
     
  20. Vasilis-Mbakalis

    Vasilis-Mbakalis

    Joined:
    May 23, 2017
    Posts:
    16
    Hello! I want to ask if its possible to have access to local files saved on an iOS device.
     
  21. unity_C6nHr3Vb_zkRqA

    unity_C6nHr3Vb_zkRqA

    Joined:
    Sep 12, 2018
    Posts:
    1
    Hey! I got a Problem with your Browser... I am developing a VR-application. For this purpose i need to set the Canvas of the SimpleFileBrowserCanvas to Render Mode 'World Space'. After that i have to asign my Main-Camera to 'Event Camera', but this doesn't work and i don't know why. Is there a different way to set my Camera to the Event Camera?
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Vasilis-Mbakalis Yes it is! Local files directory is actually the initial directory that the file browser will show on iOS because trying to browse other directories usually throws security exceptions on iOS.

    @unity_C6nHr3Vb_zkRqA I don't really know how VR apps interact with world space UI objects but file browser does support world space rendering. If you need to assign an Event Camera for VR support, drag&drop SimpleFileBrowserCanvas to your scene and assign the camera to its Event Camera. Then, in the Start function of one of your scripts, add this line:
    FileBrowser.HideDialog();
    . This way, SimpleFileBrowser will use the instance in the scene.
     
  23. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Hi yasirkula, how can I get the name of the selected file in the OnPoibterClick method, just when I give click to select a file?
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi, FileBrowserItem's Name property stores the name of the file that this item is displaying. If you'd like to raise an event when a file is clicked, you should do so inside FileBrowser.OnItemSelected function. Here, you can get the name of the selected file via item.Name.
     
  25. NocSchecter

    NocSchecter

    Joined:
    Aug 19, 2015
    Posts:
    33
    Thanks! its work!

    Thanks! its work!
     
  26. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello,

    Thanks a lot for the file and Folder browser, it is a great Help!

    I have a refresh problem: I use the file loader to Load a large file. It stays on top until it is loaded (around 10 seconds).
    I would like to hide it and Show a wait indicator, I put some waiting stuff into the method which is started from "onSuccess" but the file browser always stays on the top until it is completlty loaded.

    What is the best and fastest way to hide it?

    Thanks :)
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You should wait for 1 frame before loading the file because the UI is refreshed in the next frame. You can do so via
    yield return null;
    in a coroutine. Try adding one more
    yield return null;
    , if one doesn't work.
     
    Firlefanz73 likes this.
  28. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Thanks for the hint :)
     
  29. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello yasirkula,

    I tried to wait to a second before loading the file but still the file Folder stays on top until it is loaded completly...

    FileBrowser.ShowLoadDialog(onSuccess, onCancel);

    ...

    Code (CSharp):
    1.         private void onSuccess(string path)
    2.         {
    3.             //WaitAndSleep(1);
    4.             IWaitAndSleep(1);
    5.             CADLoader.Instance.CADLoad(path);
    6.         }
    7.         System.Threading.Tasks.Task WaitAndSleep(float waitTime)
    8.         {
    9.             System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(waitTime))
    10.                 .ContinueWith(t => System.Threading.Thread.Sleep(100));
    11.             return null;
    12.         }
    13.         IEnumerator IWaitAndSleep(float waitTime)
    14.         {
    15.             yield return new WaitForSeconds(1);
    16.             //yield return null;
    17.             //yield return null;
    18.         }
    Any idea what's wrong with my wait? Or do I need to hide it before I wait?

    Thanks a lot!
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You should call the IWaitAndSleep function via StartCoroutine (i.e.
    StartCoroutine(IWaitAndSleep(1))
    ). Try this one:

    Code (CSharp):
    1. private void onSuccess(string path)
    2. {
    3.     StartCoroutine(IWaitAndSleep(1, path));
    4. }
    5.  
    6. IEnumerator IWaitAndSleep(float waitTime, string path)
    7. {
    8.     yield return new WaitForSeconds(1);
    9.     CADLoader.Instance.CADLoad(path);
    10. }
     
    Firlefanz73 likes this.
  31. Coolsonickirby

    Coolsonickirby

    Joined:
    May 27, 2013
    Posts:
    6
    Is there any way to get the full directory to item line? (for example, file:///C:/Example.png)
     
  32. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    That's perfect. Thanks a lot :)
     
    yasirkula likes this.
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Coolsonickirby Do you mean writing the full path next to each file/folder in the file browser (instead of just the names)? Or are you simply asking how to get the full path of the selected file?
     
  34. Coolsonickirby

    Coolsonickirby

    Joined:
    May 27, 2013
    Posts:
    6
    The full path of the selected file.
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Coolsonickirby likes this.
  36. Coolsonickirby

    Coolsonickirby

    Joined:
    May 27, 2013
    Posts:
    6
    Thank you so much! It took me a while to figure it out. Good luck on your future endeavors!
     
  37. tbigfish88

    tbigfish88

    Joined:
    Nov 9, 2015
    Posts:
    35
    Hey, I'm having a problem getting this to work on android. It works fine in Unity on my Windows PC, but when I load the apk onto my device, I can browse the folder OK, but the default directory "/storage/emulated/0" appears to be empty. If I navigate back to the root I can see folders there, but my files are on the internal storage.
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Did you set "Write Permission" to "External (SDCard)" in Player Settings?
     
  39. Faisalimran

    Faisalimran

    Joined:
    Aug 18, 2015
    Posts:
    2
    What platforms it supports ? I need following platforms support
    (Mac, Windows, Android, iOS, WebGL)
     
  40. Shulman

    Shulman

    Joined:
    Jun 22, 2015
    Posts:
    4
    Hi! Thanks for sharing your browser. I'm trying to set up this asset in VR. In ScreenSpace Overlay all works fine, but in WorldSpace not showing files in viewport. Do you have any ideas on this?
     

    Attached Files:

  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    @Faisalimran It should work pretty well on Windows and Android. Didn't test it on Mac but it is supposed to work on Mac, as well. On iOS, only the local directory of the application can be browsed because iOS prevents us from accessing the other directories in the file system. WebGL is unfortunately not supported.

    @Shulman Does clicking a quick link (e.g. Documents) refresh the files, or are there any error messages in the Console in World Space mode?.
     
    Faisalimran likes this.
  42. Shulman

    Shulman

    Joined:
    Jun 22, 2015
    Posts:
    4
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hmm, I honestly have no clue why this happens or why there are no errors in the console.
     
  44. Shanmf3d

    Shanmf3d

    Joined:
    Aug 1, 2017
    Posts:
    8
    Hi yasirkula, I was wondering how easy would it be to hide the file extension, so only the file name is shown?
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Should be as simple as changing this line as
    nameText.text = System.IO.Path.GetFilenameWithoutExtension(name);


    P.S. No, it is not as simple as that. You should change the definition of Name as
    public string Name { get; private set; }
    and add
    Name = name;
    inside SetFile function (in addition to the original change I've suggested).
     
    Shanmf3d likes this.
  46. Shanmf3d

    Shanmf3d

    Joined:
    Aug 1, 2017
    Posts:
    8
    @yasirkula Thank you for your speedy response. Great stuff, I'll try it in the morning when I get back into work.

    Many thanks.
     
    yasirkula likes this.
  47. Shanmf3d

    Shanmf3d

    Joined:
    Aug 1, 2017
    Posts:
    8
  48. scottstoll2017

    scottstoll2017

    Joined:
    Feb 8, 2019
    Posts:
    3
    The imgeloader demo has twice deleted a bunch of images in the directory where I tried to load from. I cannot find them anywhere and they seem to be lost forever.

    I thought it was a mistake the first time, it wasn't. I don't know what's happened with it, but something is seriously wrong with it deleting files it was never told to touch.

    Not. Happy.
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  50. gigolv

    gigolv

    Joined:
    Sep 30, 2017
    Posts:
    6
    Hi. Great app!

    Im trying to browse the files from a shared folder on a PC on the same network, but I can't seem to point the file browser to the shared folder... is there any way to specify the initial path to that shared folder? example: "\\design\classroom\examples\design.jpg". The computer design has a shared folder called classroom with a sub folder named examples... I have multiple files in that directory which I would like to convert to a texture in my app and view them.

    Thanks again for any suggestions or feedback.