System.MethodAccessException when initializing FileStream Windows Phone 7
I'm making a WP7 that takes an image from either the gallery or the camera and by pressing a button sends it to a webservice by encoding it to a base64 string. I'm currently using the WP7 Emulator included in VS2010.
To do this, I try to use a FileStream object that will open the image stored in the image path. However, when I try to initialize the FileStream, I get in the console the message:
A first chance exception of type 'System.MethodAccessException' occurred in LiveAndesApp.dll
'taskhost.exe' (Managed): Loaded 'System.ServiceModel.Web.dll'
A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Followed by lots and lots of the System.Xml.XmlException. The weird thing is that I put the FileStream creation in a try-catch statement that catches System.MethodAccessException and E, and the program doesn't even enter it, it just goes on with the sendSighting
What am I doing wrong and how can I improve this? Thanks a lot!
Here is the complete code. This is how I call the method for converting the picture.
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)); }
This is the code for the Controller class. I omitted everything related to the web request for the sake of brevity:
public class Controller
{
public Controller()
{ }
/// <summary>
/// Manda un avistamiento al servicio.
/// </summary>
public void sendSighting()
{
//Obtenemos el a开发者_运维知识库vistamiento
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;
}
}
Thank you for your time! :D
Use Isolated Storage instead of System.IO
FileStream is part of System.IO namespace which can be replaced with smth like IsolatedStorageFileStream
Link with help
精彩评论