I am having trouble getting Unity to locate and read a video file located on the SD card of an Android Device.
When the video file is saved internally on the device itself the persistentDataPath can find and read the file for Unity (the file structure looks something like this: "Android/data/com.name.name/files/video.mp4"). However when the video file is on an SD card and WRITE_EXTERNAL_STORAGE permission is enabled my "File.Exists" returns false.
I believe the problem is that persistentDataPath doesn't point to the same location on the SD card. I've tried for several hours to find a successful file structure but have so far been unsuccessful. (some of the file structures I've tried are: "Android/data/com.name.name/files/video.mp4", "Android/com.name.name/files/video.mp4", "Android/data/com.name.name/video.mp4", "Android/com.name.name/video.mp4").
The only other thing that I can think of that would be causing this issue is for some reason persistentDataPath is ignoring the WRITE_EXTERNAL_STORAGE permission and is looking at its internal path where obviously the video file no longer exists.
c#:
private string videoFilePath;
private void Start()
{
videoFilePath = Application.persistentDataPath + "/videos/video.mp4";
if (File.Exists(videoFilePath)
{
gameObject.GetComponent<VideoPlayer>().url =
videoFilePath;
}
else
{
GameObject.Find("GUI Text").GetComponentInParent<Text>().text =
"Cant find selected video";
}
}
AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Java in c#:
GetAndroidContextExternalFilesDir()
{
if (Application.platform == RuntimePlatform.Android)
{
try
{
using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
{
path = ajo.Call<AndroidJavaObject>("getExternalFilesDir", null).Call<string>("getAbsolutePath");
}
}
}
catch (Exception e)
{
GameObject.Find("GUI Text").GetComponentInParent<Text>().text = "Error fetching native Android external storage dir: " + e.Message;
}
}
return path;
}