我在unity3d开发了一个简单的安卓游戏,我想读一个由我自己创建的文件。这是一个简单的配置文件。 异常调用堆栈:
IsolatedStorageException:找不到路径“/storage/emulated/0/Android/data/com.Test.Demo/files/basic.cfg”的一部分。
07-16 12:04:35.905 4248 5019 I Unity:
在System.IO.FileStream..ctor(System.String路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,布尔匿名,FileOptions选项)[0x00000] in:007-16 12:04:35.905 4248 5019 I Unity:在System.IO.FileStream..ctor(System.String路径,FileMode模式,FileAccess访问,FileShare共享)[0x00000] in:0
07-16 12:04:35.905 4248 5019 I Unity:在System.IO.File.OpenRead(System.String path)[0x00000] in:0
07-16 12:04:35.905 4248 5019 I Unity:at System.IO.File.ReadAllBytes(System.String path)[0x00000] in:0
07-16 12:04:35.905 4248 5019 I Unity:在FileManager.ReadFile(System.String filePath)[0x00000] in:0 **
这是我的阅读文件代码。
public static byte[] ReadFile(string filePath)
{
if (!System.IO.File.Exists(filePath))
{
return null;
}
return System.IO.File.ReadAllBytes(filePath);
}
以下是我收到的异常消息:
IsolatedStorageException:找不到路径“/storage/emulated/0/Android/data/com.Test.Demo/files/basic.cfg”的一部分。
我不明白为什么即使ReadAllBytes
测试通过,File.Exists()
方法也会抛出此异常。
这是我在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 [size];
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;
}
}
这是我的manifest.xml权限配置:
<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" />
但这并不是全部。
此异常不是每次都抛出,但有时我也会收到此异常。