- Home /
How can I browse files on Android outside of the Unity App folder?
Is there any way to access files in Android with permission from the user?
I think it could be possible using an Android Manifest to require permissions and then access the directories using Native Android Methods but I really don't have any experience programming directly on Android, so I was wondering what is a good point to begin with and if my hypothesis is correct
I already read this Unity manual page: Building Plugins for Android But I don't know if there is something I'm missing, can somebody please tell me what workflow I should follow to accomplish my goal? :)
PS: Excuse me if I commit some grammar or syntax errors but I'm not a native english speaker
You could consider using assets from AssetStore. This is one that I have used: https://www.assetstore.unity3d.com/en/#!/content/151
Really I doesn't like too much all the code assets in the Asset Store, because they usually are restricted to some functions and/or I doesn't have enough money yet to afford an asset, I really want to know how to do a native Android plugin without restrictions, so I can handle new features and improvements fast without trying to understand other person's code, but thanks for the suggestion
Answer by aditya · Dec 03, 2016 at 05:13 AM
Best way to accomplish this is by native Android code and you could do this by native Android plugins... There are lot of tutorials out there on how to create an Android plugin for unity and This Tutorial Here is by far the best i think coz it has nothing to with Android Studio as i m not a big fan of Android Studio ... i know Android Studio helps you with java programming but it has other complications too like gradle and all that... okay, so just follow that tutorial and create an android project and where there you are asked to create a java file just put your own code of accessing android file system (or something like this), just google for code on how to access android directories and you are good to go ... one thin i want to mention here that is not mentioned in tutorial itself is when you reach this line ant jar
, then before this just cd your project directory ... for eg, if you created your android project in C:\MyProject path then before ant jar
execute cd C:\MyProject
so that Apache Ant can find your build.xml
That tutorial seems very useful, I didn't find anything like that while searching on Google, maybe I didn't use the correct keywords haha Thanks!
Answer by WoozyBytes · Dec 28, 2016 at 08:39 AM
There's no need to create a plugin for this, you can just use .net System.IO. to create a file browser, and also use Unity's AndroidJavaClass and AndroidJavaObject to call Android Native Methods like Environment.getExternalStorageDirectory to get the path to the user storage for example.
function GetAndroidExternalStoragePath ()
{
var path : String = "";
try
{
var jc : AndroidJavaClass = new AndroidJavaClass("android.os.Environment") ;
path = jc.CallStatic.<AndroidJavaObject>("getExternalStorageDirectory").Call.<String>("getAbsolutePath");
}
catch (e)
{
Debug.Log(e.Message);
}
}
I think the correct conversion would be:
public string GetAndroidExternalStoragePath ()
{
string path = "";
try
{
AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment") ;
path = jc.CallStatic<AndroidJavaObject>("getExternalStorageDirectory").Call<string>("getAbsolutePath");
return path;
}
catch (Exception e)
{
Debug.Log(e.$$anonymous$$essage);
}
}
i need to call this function on button click in order to select files from file picker in android ?
How to reach a public directory in the internal storage? I mean, a directory that's in the same path as DCI$$anonymous$$, Downloads, Android, etc.
I know this is late, but if anyone's wondering how to accomplish that, here's the code (In case you want to access the DCI$$anonymous$$ directory, specifically. If not, just change the static property you're passing as the string parameter):
AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment");
string path = jc.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory", jc.GetStatic<string>("DIRECTORY_DCI$$anonymous$$")).Call<string>("getAbsolutePath");
Debug.Log("Attempting to recover DCI$$anonymous$$ External Storage Directory: " + path);
Thank you! I've been trying to figure out how can I get the directory to the DCI$$anonymous$$ folder but all the answers so far are either outdated, asking me to use a plug-in or asking to use persistent data for some reason.