i have developed a simple android game in unity3d, i want to read a file which is created by myself. it is a simple config file. exception call stack:
IsolatedStorageException: Could not find a part of the path "/storage/emulated/0/Android/data/com.Test.Demo/files/basic.cfg".
07-16 12:04:35.905 4248 5019 I Unity : at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
07-16 12:04:35.905 4248 5019 I Unity : at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
07-16 12:04:35.905 4248 5019 I Unity : at System.IO.File.OpenRead (System.String path) [0x00000] in <filename unknown>:0
07-16 12:04:35.905 4248 5019 I Unity : at System.IO.File.ReadAllBytes (System.String path) [0x00000] in <filename unknown>:0
07-16 12:04:35.905 4248 5019 I Unity : at FileManager.ReadFile (System.String filePath) [0x00000] in <filename unknown>:0
this is my read file code.
public static byte[] ReadFile(string filePath)
{
if (!System.IO.File.Exists(filePath))
{
return null;
}
return System.IO.File.ReadAllBytes(filePath);
}
this is my code. the exception message is :
IsolatedStorageException: Could not find a part of the path “/storage/emulated/0/Android/data/com.Test.Demo/files/basic.cfg”.
i can’t understand that why the ReadAllBytes method throw this exception, even if it is passed by File.Exists test.
this is the code i find in unity.mono:
public static FileStream OpenRead (string path)
{
return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
//
// The documentation for this method is most likely wrong, it
// talks about doing a "binary read", but the remarks say
// that this "detects the encoding".
//
// This can not detect and do anything useful with the encoding
// since the result is a byte [] not a char [].
//
public static byte [] ReadAllBytes (string path)
{
using (FileStream s = OpenRead (path)) {
long size = s.Length;
// limited to 2GB according to http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx
if (size > Int32.MaxValue)
throw new IOException ("Reading more than 2GB with this call is not supported");
int pos = 0;
int count = (int) size;
byte [] result = new byte ;
while (count > 0) {
int n = s.Read (result, pos, count);
if (n == 0)
throw new IOException ("Unexpected end of stream");
pos += n;
count -= n;
}
return result;
}
}
this is my manifest.xml permission configuration:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
not all of them.
and this exception is not throw every times. sometimes it throw this exception.