初始化FileStream Windows Phone 7时出现System.MethodAccessException

时间:2011-10-08 20:17:05

标签: c# .net exception windows-phone-7 filestream

我正在制作一个WP7,它从图库或相机中获取图像,然后按一个按钮,通过将其编码为base64字符串将其发送到Web服务。我目前正在使用VS2010中包含的WP7仿真器。

为此,我尝试使用FileStream对象来打开存储在图像路径中的图像。但是,当我尝试初始化FileStream时,我会在控制台中收到消息:

  

类型'System.MethodAccessException'的第一次机会异常   发生在LiveAndesApp.dll

     

'taskhost.exe'(已管理):已加载'System.ServiceModel.Web.dll'

     

第一个   发生了'System.Xml.XmlException'类型的机会异常   system.xml.dll的

接下来是很多System.Xml.XmlException。奇怪的是,我将FileStream创建放在一个捕获System.MethodAccessException和E的try-catch语句中,程序甚至没有输入它,它只是继续使用sendSighting

我做错了什么,我该如何改进?非常感谢!

这是完整的代码。这就是我称之为转换图片的方法。

public void next_Click(object sender, EventArgs e)
        {
            //Dependera de si seguimos flujos offline y online. 
            if (!offline_mode)
            {
                NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=online", UriKind.Relative));
                Controller c = new Controller();
                c.sendSighting();
            }
            else { NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=offline", UriKind.Relative)); }

这是Controller类的代码。为简洁起见,我省略了与Web请求相关的所有内容:

    public class Controller
    {
        public Controller()
        { }

        /// <summary>
        /// Manda un avistamiento al servicio.
        /// </summary>
        public void sendSighting()
        {
            //Obtenemos el avistamiento
            AddSightingFlowObject flow_object = AddSightingFlowObject.getInstance();

            //Creamos el objeto json y lo incorporamos al body del request.
            JObject json = new JObject();

            //Si la imagen no es nula, tenemos que procesarla.
            JArray arr = new JArray(new List<String>());
            if (flow_object.ImageControl != null)
            {
                String image_base_64 = ConvertImageToBase64(flow_object.ImagePath);
                arr.Add(image_base_64);
            }
            else
            {
                arr.Add("");
            }
            json.Add("PhotoURL", arr);
        }


        public String ConvertImageToBase64(String imagePath)
        {
            String image_base_64 = "";
            FileStream fs;
            Byte[] arr;

            try
            {
                fs = new FileStream(imagePath, FileMode.Open);
                arr = new Byte[fs.Length];
                fs.Read(arr, 0, arr.Length);
                image_base_64 = System.Convert.ToBase64String(arr);
            }
            catch (System.MethodAccessException e)
            {
                String error = "Error: " + e.Message + "Stack Trace: " + e.StackTrace;
            }

            return image_base_64;
        }

}

感谢您的时间! :d

1 个答案:

答案 0 :(得分:1)

使用隔离存储而不是System.IO

FileStream是System.IO命名空间的一部分,可以用像IsolatedStorageFileStream这样的smth替换

Link with help