Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 20, 2014 at 05:05 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by LijuDeveloper · Nov 14, 2013 at 11:09 AM · filesystemlocalfilesystemfilebrowser

Accessing local system ( File Browser )

How to access a file from local system using Application. For example how can i access a photo from my system for uploading .

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Nov 14, 2013 at 11:57 AM 1
Share

Application class won't help you in this case. To access local files you have to use other classes like WWW or standard .NET classes like File, BinaryReader, etc.

Regarding uploading: please take a look at this answer from Bunny83

avatar image LijuDeveloper · Nov 16, 2013 at 05:07 AM 0
Share

Any one know any other option ?

avatar image ArkaneX · Nov 16, 2013 at 10:18 AM 0
Share

Care to explain why you need other option?

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by LijuDeveloper · Nov 20, 2014 at 04:52 AM

i've got the solution. I got a free plugin for file browser . C# File Browser in Asset Store. The code is

         #define thread //comment out this line if you would like to disable multi-threaded search
         using UnityEngine;
         using System.Collections;
         using System.IO;
         using System.Collections.Generic;



         #if thread
         using System.Threading;
         #endif


         public class FileBrowser{
         //public 
             //Optional Parameters
             public string name = "File Browser"; //Just a name to identify the file browser with
             //GUI Options
             public GUISkin guiSkin; //The GUISkin to use
             public int layoutType{    get{    return layout;    }    } //returns the current Layout type
             public Texture2D fileTexture,directoryTexture,backTexture,driveTexture; //textures used to represent file types
             public GUIStyle backStyle,cancelStyle,selectStyle; //styles used for specific buttons
             public Color selectedColor = new Color(0.5f,0.5f,0.9f); //the color of the selected file
             public bool isVisible{    get{    return visible;    }    } //check if the file browser is currently visible
             //File Options

             //Output
             public FileInfo outputFile; //the selected output file
             //Search
             public bool showSearch = false; //show the search bar
             public bool searchRecursively = false; //search current folder and sub folders
         //Protected    
             //GUI
             protected Vector2 fileScroll=Vector2.zero,folderScroll=Vector2.zero,driveScroll=Vector2.zero;
             protected Color defaultColor;
             protected int layout;
             protected Rect guiSize;
             protected GUISkin oldSkin;
             protected bool visible = false;
             //Search
             protected string searchBarString = ""; //string used in search bar
             protected bool isSearching = false; //do not show the search bar if searching
             //File Information
             protected DirectoryInfo currentDirectory;
             protected FileInformation[] files;
             protected DirectoryInformation[] directories,drives;
             protected DirectoryInformation parentDir;
             protected bool getFiles = true,showDrives=false;
             protected int selectedFile = -1;
             //Threading
             protected float startSearchTime = 0f;
             #if thread
             protected Thread t;
             #endif
             
             //Constructors
             public FileBrowser(string directory,int layoutStyle,Rect guiRect){    currentDirectory = new DirectoryInfo(directory);    layout = layoutStyle;    guiSize = guiRect;    }
             #if (UNITY_IPHONE || UNITY_ANDROID || UNITY_BLACKBERRY || UNITY_WP8)
             public FileBrowser(string directory,int layoutStyle):this(directory,layoutStyle,new Rect(0,0,Screen.width,Screen.height)){}

                 public FileBrowser(string directory):this(directory,1){}
             #else
                 public FileBrowser(string directory,int layoutStyle):this(directory,layoutStyle,new Rect(0,0,Screen.width,Screen.height)){}
             //public FileBrowser(string directory,int layoutStyle):this(directory,layoutStyle,new Rect(Screen.width*0.125f,Screen.height*0.125f,Screen.width*0.75f,Screen.height*0.75f)){}
                 public FileBrowser(string directory):this(directory,0){}
             #endif
             public FileBrowser(Rect guiRect):this(){    guiSize = guiRect;    }
             public FileBrowser(int layoutStyle):this(Directory.GetCurrentDirectory(),layoutStyle){}
             public FileBrowser():this(Directory.GetCurrentDirectory()){}
             
             //set variables
             public void setDirectory(string dir){    currentDirectory=new DirectoryInfo(dir);    }
             public void setLayout(int l){    layout=l;    }
             public void setGUIRect(Rect r){    guiSize=r;    }
             
             
             //gui function to be called during OnGUI
             public bool draw(){
                 if(getFiles){
                     getFileList(currentDirectory); 
                     getFiles=false;
                 }
                 if(guiSkin){
                     oldSkin = GUI.skin;
                     GUI.skin = guiSkin;
                 }
                 GUILayout.BeginArea(guiSize);
                 GUILayout.BeginVertical("box");
                 switch(layout){
                     case 0:
                         GUILayout.BeginHorizontal("box");
                             GUILayout.FlexibleSpace();
                             GUILayout.Label(currentDirectory.FullName);
                             GUILayout.FlexibleSpace();
         //                    if(showSearch){
         //                        drawSearchField();
         //                        GUILayout.Space(10);
         //                    }
                         GUILayout.EndHorizontal();
                         GUILayout.BeginHorizontal("box");
                             GUILayout.BeginVertical(GUILayout.MaxWidth(300));
                             
                                 folderScroll = GUILayout.BeginScrollView(folderScroll);
                                 if(showDrives){
                                     foreach(DirectoryInformation di in drives){
                                         if(di.button()){    getFileList(di.di);    }
                                     }
                                 }else{
                                     if((backStyle != null)?parentDir.button(backStyle):parentDir.button())
                                         getFileList(parentDir.di);
                                 }
                                 foreach(DirectoryInformation di in directories){
                                     if(di.button()){    getFileList(di.di);    }
                                 }
                                 GUILayout.EndScrollView();
                             GUILayout.EndVertical();
                             GUILayout.BeginVertical("box");
                                 if(isSearching){
                                     drawSearchMessage();
                                 }else{
                                     fileScroll = GUILayout.BeginScrollView(fileScroll);
                                     for(int fi=0;fi<files.Length;fi++){
                                         if(selectedFile==fi){

                                             defaultColor = GUI.color;
                                             GUI.color = selectedColor;
                                         }
                                         if(files[fi].button()){
                                             outputFile = files[fi].fi;
                                             selectedFile = fi;
                                         }
                                         if(selectedFile==fi)
                                             GUI.color = defaultColor;
                                     }
                                     GUILayout.EndScrollView();
                                 }
                                 GUILayout.BeginHorizontal("box");
                                 GUILayout.FlexibleSpace();
                     if((cancelStyle == null)?GUILayout.Button("Cancel"):GUILayout.Button("Cancel",cancelStyle)){  //
                         outputFile = null;
                                     return true;
                                 }
                                 GUILayout.FlexibleSpace();
                     if((selectStyle == null)?GUILayout.Button("Select"):GUILayout.Button("Select",selectStyle)){ c_global.fileFullName = currentDirectory.FullName ; return true;    }
                                 GUILayout.FlexibleSpace();
                                 GUILayout.EndHorizontal();
                             GUILayout.EndVertical();
                         GUILayout.EndHorizontal();
                         break;
                     case 1: //mobile preferred layout
                     default:
                         if(showSearch){
                             GUILayout.BeginHorizontal("box");
                                 GUILayout.FlexibleSpace();
                                 GUILayout.Label(currentDirectory.Name);
                                 //drawSearchField();
                                 GUILayout.FlexibleSpace();
                             GUILayout.EndHorizontal();
                         }
                         fileScroll = GUILayout.BeginScrollView(fileScroll);
                         
                         if(isSearching){
                             drawSearchMessage();
                         }else{
                             if(showDrives){
                                 GUILayout.BeginHorizontal();
                             GUILayout.FlexibleSpace();
                                 foreach(DirectoryInformation di in drives){
                                     if(di.button()){    getFileList(di.di);    }
                                 }
                             GUILayout.FlexibleSpace();
                                 GUILayout.EndHorizontal();
                             }else{
                                 if((backStyle != null)?parentDir.button(backStyle):parentDir.button())
                                     getFileList(parentDir.di);
                             }
                             
                             
                             foreach(DirectoryInformation di in directories){

                                 if(di.button()){    getFileList(di.di);    }
                             }
                             for(int fi=0;fi<files.Length;fi++){
                                 if(selectedFile==fi){
                                     defaultColor = GUI.color;
                                     GUI.color = selectedColor;
                                 }
                                 if(files[fi].button()){
                                     outputFile = files[fi].fi;
                                     selectedFile = fi;
                                 }
                                 if(selectedFile==fi)
                                     GUI.color = defaultColor;
                             }
                         }
                         GUILayout.EndScrollView();

                     GUILayout.BeginHorizontal("box");
                     GUILayout.FlexibleSpace();
                     if((cancelStyle == null)?GUILayout.Button("Cancel"):GUILayout.Button("Cancel",cancelStyle)){  //
                         outputFile = null;
                         return true;
                     }
                     GUILayout.FlexibleSpace();
                     if((selectStyle == null)?GUILayout.Button("Select"):GUILayout.Button("Select",selectStyle)){c_global.fileFullName = currentDirectory.FullName ;    return true;    }
                     GUILayout.FlexibleSpace();
                     GUILayout.EndHorizontal();


         //            if((selectStyle == null)?GUILayout.Button("Select"):GUILayout.Button("Select",selectStyle)){    return true;    }
         //            GUILayout.FlexibleSpace();
         //            if((cancelStyle == null)?GUILayout.Button("Cancel"):GUILayout.Button("Cancel",cancelStyle)){
         //                    outputFile = null;
         //                    return true;
         //                }
         //
                 break;
                 }
                 GUILayout.EndVertical();
                 GUILayout.EndArea();
                 if(guiSkin){GUI.skin = oldSkin;}
                 return false;
             }
             
             protected void drawSearchField(){
                 if(isSearching){
                     GUILayout.Label("Searching For: \""+searchBarString+"\"");
                 }else{
                     searchBarString = GUILayout.TextField(searchBarString,GUILayout.MinWidth(150));
                     if(GUILayout.Button("search")){
                         if(searchBarString.Length > 0){
                             isSearching = true;
                             #if thread
                             startSearchTime = Time.time;
                             t = new Thread(threadSearchFileList);
                             t.Start(true);
                             #else
                             searchFileList(currentDirectory);
                             #endif
                         }else{
                             getFileList(currentDirectory);

                         }
                     }
                 }
             }
             
             protected void drawSearchMessage(){
                 float tt = Time.time-startSearchTime;
                 if(tt>1)
                     GUILayout.Button("Searching");
                 if(tt>2)
                     GUILayout.Button("For");
                 if(tt>3)
                     GUILayout.Button("\""+searchBarString+"\"");
                 if(tt>4)
                     GUILayout.Button(".....");
                 if(tt>5)
                     GUILayout.Button("It's");
                 if(tt>6)
                     GUILayout.Button("Taking");
                 if(tt>7)
                     GUILayout.Button("A");
                 if(tt>8)
                     GUILayout.Button("While");
                 if(tt>9)
                     GUILayout.Button(".....");
             }
             
             public void getFileList(DirectoryInfo di){
                 //set current directory
                 currentDirectory = di;
                 //get parent
                 if(backTexture)
                     parentDir = (di.Parent==null)?new DirectoryInformation(di,backTexture):new DirectoryInformation(di.Parent,backTexture);
                 else
                     parentDir = (di.Parent==null)?new DirectoryInformation(di):new DirectoryInformation(di.Parent);
                 showDrives = di.Parent==null;



                 //get drives
                 string[] drvs = System.IO.Directory.GetLogicalDrives();
                 drives = new DirectoryInformation[drvs.Length];
                 for(int v=0;v<drvs.Length;v++){
                     drives[v]= (driveTexture==null)?new DirectoryInformation(new DirectoryInfo(drvs[v])):new DirectoryInformation(new DirectoryInfo(drvs[v]),driveTexture);
                 }
                 
                 //get directories
                 DirectoryInfo[] dia = di.GetDirectories();
                 directories = new DirectoryInformation[dia.Length];
                 for(int d=0;d<dia.Length;d++){
                     if(directoryTexture)
                         directories[d] = new DirectoryInformation(dia[d],directoryTexture);
                     else
                         directories[d] = new DirectoryInformation(dia[d]);
                 }


                 FileInfo [] f1 = di.GetFiles ("*.jpg");
                 FileInfo [] f2 = di.GetFiles ("*.jpeg");
                 FileInfo [] f3 = di.GetFiles ("*.png");

                 int len1 = f1.Length;
                 int len2 = f2.Length;
                 int len3 = f3.Length;

                 int tot = len1+len2+len3 ;

                 FileInfo[] f4 = new FileInfo[tot];
                 int xx =0;

                 foreach(FileInfo f in f1)
                 {
                     f4[xx] = f;
                     xx++;
                 }
                 foreach(FileInfo f in f2)
                 {
                     f4[xx] = f;
                     xx++;
                 }
                 foreach(FileInfo f in f3)
                 {
                     f4[xx] = f;
                     xx++;
                 }


             
                 FileInfo[] fia = f4 ;//searchDirectory(di,searchPattern);

                 files = new FileInformation[fia.Length];
                 for(int f=0;f<fia.Length;f++){
                 

                     if(fileTexture)
                         files[f] = new FileInformation(fia[f],fileTexture);
                     else
                         files[f] = new FileInformation(fia[f]);
                 }
             }


             
             public void searchFileList(DirectoryInfo di){
                 searchFileList(di,fileTexture!=null);
             }
             
             protected void searchFileList(DirectoryInfo di,bool hasTexture){
                 //(searchBarString.IndexOf("*") >= 0)?searchBarString:"*"+searchBarString+"*"; //this allows for more intuitive searching for strings in file names
                 FileInfo[] fia = di.GetFiles((searchBarString.IndexOf("*") >= 0)?searchBarString:"*"+searchBarString+"*",(searchRecursively)?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
                 files = new FileInformation[fia.Length];
                 for(int f=0;f<fia.Length;f++){
                     if(hasTexture)
                         files[f] = new FileInformation(fia[f],fileTexture);
                     else
                         files[f] = new FileInformation(fia[f]);
                 }
                 #if thread
                 #else
                 isSearching = false;
                 #endif
             }
             
             protected void threadSearchFileList(object hasTexture){
                 searchFileList(currentDirectory,(bool)hasTexture);
                 isSearching = false;
             }
             
             //search a directory by a search pattern, this is optionally recursive
             public static FileInfo[] searchDirectory(DirectoryInfo di,string sp,bool recursive){
                 return di.GetFiles(sp,(recursive)?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
             }
             public static FileInfo[] searchDirectory(DirectoryInfo di,string sp){
                 return searchDirectory(di,sp,false);
             }
             
             public float brightness(Color c){    return    c.r*.3f+c.g*.59f+c.b*.11f;    }
             
             //to string
             public override string ToString(){
                 return "Name: "+name+"\nVisible: "+isVisible.ToString()+"\nDirectory: "+currentDirectory+"\nLayout: "+layout.ToString()+"\nGUI Size: "+guiSize.ToString()+"\nDirectories: "+directories.Length.ToString()+"\nFiles: "+files.Length.ToString();
             }
         }

         public class FileInformation{
             public FileInfo fi;
             public GUIContent gc;
             
             public FileInformation(FileInfo f){
                 fi=f;
                 gc = new GUIContent(fi.Name);
             }
             
             public FileInformation(FileInfo f,Texture2D img){
                 fi = f;
                 gc = new GUIContent(fi.Name,img);
             }


             public bool button(){return GUILayout.Button(gc);}
             public void label(){    GUILayout.Label(gc);    }
             public bool button(GUIStyle gs){return GUILayout.Button(gc,gs);}
             public void label(GUIStyle gs){    GUILayout.Label(gc,gs);    }
         }

         public class DirectoryInformation{
             public DirectoryInfo di;
             public GUIContent gc;
             
             public DirectoryInformation(DirectoryInfo d){
                 di=d;
                 gc = new GUIContent(d.Name);
             }
             
             public DirectoryInformation(DirectoryInfo d,Texture2D img){
                 di=d;
                 gc = new GUIContent(d.Name,img);
             }
             
             public bool button(){return GUILayout.Button(gc);}
             public void label(){    GUILayout.Label(gc);    }
             public bool button(GUIStyle gs){return GUILayout.Button(gc,gs);}
             public void label(GUIStyle gs){    GUILayout.Label(gc,gs);    }
         }


For Using this browser use folowing code

             using UnityEngine;
             using System.Collections;
             using System;
             using System.IO;

             public class testFileBrowser : MonoBehaviour {
             //skins and textures
             public GUISkin[] skins;
             public Texture2D file,folder,back,drive;



             string[] layoutTypes = {"Type 0","Type 1"};
             //initialize file browser

             FileBrowser fb = new FileBrowser();

             string output = "no file";

             private string  defaultFileDirectory =    "/sdcard/DCIM/Camera";
             //    private string  defaultFileDirectory2 =    "/sdcard/DCIM";
             //    private string  defaultFileDirectory3 = "/sdcard";
             //    private string  defaultFileDirectory4 = "/";

             // Use this for initialization
             void OnEnable () {
                 //setup file browser style
                 print("bbhbh");
                 fb.guiSkin = skins[0]; //set the starting skin

                 if(Directory.Exists(defaultFileDirectory) )
                 {
                     fb.setDirectory(defaultFileDirectory);
                 }
             //        else if(Directory.Exists(defaultFileDirectory2) )
             //        {
             //            fb.setDirectory(defaultFileDirectory2);
             //        }
             //        else if(Directory.Exists(defaultFileDirectory3) )
             //        {
             //            fb.setDirectory(defaultFileDirectory3);
             //        }
             //        else
             //        {
             //            fb.setDirectory(defaultFileDirectory4);
             //        }




                 //set the various textures
                 //fb.searchFileList((DirectoryInformation)defaultFileDirectory);
                 fb.fileTexture = file; 
                 fb.directoryTexture = folder;
                 fb.backTexture = back;
                 fb.driveTexture = drive;
                 //show the search bar
                 fb.showSearch = true;
                 //search recursively (setting recursive search may cause a long delay)
                 fb.searchRecursively = true;
             }

             void Update()
             {

             }
             void OnGUI(){

                                                                 //draw and display output
                 if(fb.draw()){ //true is returned when a file has been selected
                                                             //the output file is a member if the FileInfo class, if cancel was selected the value is null

                     output = (fb.outputFile==null)?"":fb.outputFile.ToString();

                     if(fb.outputFile !=null )
                     {
                         float fSize = fb.outputFile.Length ;
                         print("File Size :"+fSize   );


                             #if UNITY_EDITOR
                                 c_global.fileFullName= fb.outputFile.ToString ();
                             #else 
                     
                             //c_global.fileFullName ="\\"+fb.outputFile.FullName.ToString ();;
                         c_global.fileFullName =fb.outputFile.FullName.ToString ();;

                             #endif 


                     }
                     else
                     {
                         c_global.fileFullName ="";
                     }
                     c_global.isFileBrowserHide = true; 
                 
                 }
             }







                 



             }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by GameVortex · Nov 14, 2013 at 11:53 AM

The Application class does not have a way of loading files from the local file system. Instead you could use the WWW class with the file:// protocol in a coroutine like this:

 public IEnumerator LoadImageFromLocalFile()
 {
     WWW www = new WWW("file://" + pathToLocalFileWithExtension);
     yield return www;
     
     Texture2D texture = www.texture;
 }

Keep in mind that the image must be either .png or .jpg.

More on the WWW class here: WWW

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BlackPanda · Mar 25, 2015 at 09:23 AM 0
Share

Thanks to both the above answers. @LijuDeveloper's method needs @GameVortex's method to complete the process.

avatar image worlaz · Oct 16, 2015 at 01:27 PM 0
Share

ImageFiles are not getting loaded on Android. Any reason? Am using www method.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do I load a data file using a relative path? 2 Answers

Visual Studio tells me I cant use File.WriteAllBytes or System.IO.File.WriteAllBytes? 1 Answer

Creating a Directory in "My Documents" 2 Answers

I can't place a script in the objects. 0 Answers

How to tell android to open a file 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges