I create a Xamarin app and i want to serialize some data.
FileWorker class for Android:
public class FileWorker : IFileWorker
{
public Task<IEnumerable<string>> GetFilesAsync(string folder)
{
IEnumerable<string> filenames = from filepath in Directory.EnumerateFiles(GetDocsPath() + "/" + folder)
select Path.GetFileName(filepath);
return Task<IEnumerable<string>>.FromResult(filenames);
}
string GetFilePath(string filename)
{
return Path.Combine(GetDocsPath(), filename);
}
string GetDocsPath()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
}
public Task SerializeAsync<T>(string filename, T obj)
{
string filepath = GetFilePath(filename);
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
formatter.WriteObject(fs, obj);
return Task.FromResult(true);
}
public Task<T> DeserializeAsync<T>(string filename)
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
T obj;
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
obj = (T)formatter.ReadObject(fs);
return Task.FromResult(obj);
}
}
And Add method:
public async void AddObjectAsync(object obj)
{
var fv = DependencyService.Get<IFileWorker>();
string path = AppSettings.ObjectsFolder + "/";
try
{
await fv.SerializeAsync(path + "obj", obj);
}
catch (Exception e) { }
}
But i catch Exception in this function:
System.IO.DirectoryNotFoundException
How to create this directory on Android (and IOS, if same Exception is possible)?
you can use the normal C# System.IO methods
using System.IO;
DirectoryInfo di = Directory.CreateDirectory(path);
Related
EDIT: sorry the code is working as intended. I just failed to test properly. Sorry for your inconvenience
I found some code here on SO to store and load objects (Code in the end). Storing the file is correctly working, but getting the file into object back again is not working when you have a list of objects:
Executing
Block b = loadFile<Block>("file");
Console.WriteLine(b.allCoins.Count); //is 0
results in an empty List. Checking the xml file they are all correctly stored, which means that the loading is somehow not working. How can you correctly load the object?
Here is the block class:
[Serializable]
public class Block {
public struct Coin {
public string owner;
public string name;
public Coin(string n, string o) {
owner = o;
name = n;
}
};
public int name;
public List<string> hashOfParticles;
public int numberOfTransactions;
public List<Coin> allCoins;
}
}
Here is how i load the file into objects:
public static T loadFile<T>(string fileName) {
if (string.IsNullOrEmpty(fileName)) { return default(T); }
T objectOut = default(T);
try {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fileName);
string xmlString = xmlDocument.OuterXml;
using (StringReader read = new StringReader(xmlString)) {
Type outType = typeof(T);
XmlSerializer serializer = new XmlSerializer(outType);
using (XmlReader reader = new XmlTextReader(read)) {
objectOut = (T)serializer.Deserialize(reader);
reader.Close();
}
read.Close();
}
} catch (Exception ex) {
//Log exception here
}
return objectOut;
}
Here is the code which stores the file:
public static void storeFile<T>(T serializableObject, string fileName) {
if (serializableObject == null) { return; }
try {
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (MemoryStream stream = new MemoryStream()) {
serializer.Serialize(stream, serializableObject);
stream.Position = 0;
xmlDocument.Load(stream);
xmlDocument.Save(fileName);
stream.Close();
Form1.instance.addToLog("Storing \"" + fileName + "\" succesful");
}
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
Form1.instance.addToLog("Storing \"" + fileName + "\" NOT succesful");
}
}
Apologizes for the long title!
I'm fairly new to C# (probably only 2-3 months of concrete knowledge) that I learned in College...
I've been experimenting with Xamarin Forms XAML and I was wondering how I would write a method in the code-behind that opens the tapped image to be opened in the default image viewer for Android or iOS.
I say Android or iOS because I'm doing cross-platform Xamarin Forms PCL.
Thanks :) Happy Holidays and a Great New Year :D!
Try following code :
PCL interface :
namespace MyApp.Common.Interfaces
{
public interface IDataViewer
{
void showPhoto(string AttachmentName, byte[] AttachmentBytes);
string ImageExists(string Filename, byte[] ImageData);
}
}
Platform Specific (Droid) :
using Android.Content;
using Android.Webkit;
using Android.Widget;
using System;
using System.IO;
using MyApp.Common.Interfaces;
using Xamarin.Forms;
[assembly: Dependency(typeof(DataViewer))]
namespace MyApp.Droid.Common
{
public class DataViewer : IDataViewer
{
public void showPhoto(string AttachmentName, byte[] AttachmentBytes)
{
string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path;
var FileName = AttachmentName;
Java.IO.File file = new Java.IO.File(dirPath, FileName);
if (!file.Exists())
{
var filename = Path.Combine(dirPath, AttachmentName);
File.WriteAllBytes(filename, AttachmentBytes);
}
Device.BeginInvokeOnMainThread(() =>
{
//var oDir = Xamarin.Forms.Forms.Context.FilesDir.AbsolutePath;
Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
Intent intent = new Intent(Intent.ActionView);
String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl((string)uri).ToLower());
intent.SetDataAndType(uri, mimeType);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (System.Exception ex)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View this file", ToastLength.Short).Show();
}
});
}
public string ImageExists(string FileName, byte[] Imagedata)
{
string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path;
Java.IO.File file = new Java.IO.File(dirPath, FileName);
if (!file.Exists())
{
var filename = Path.Combine(dirPath, FileName);
File.WriteAllBytes(filename, Imagedata);
return filename;
}
else
{
var filename = Path.Combine(dirPath, FileName);
return filename;
}
}
}
}
Platform Specific (iOS) :
using Foundation;
using QuickLook;
using System;
using System.IO;
using UIKit;
using MyApp.Common.Interfaces;
[assembly: Dependency(typeof(DataViewer))]
namespace MyApp.iOS.Common
{
public class DataViewer : IDataViewer
{
public void showPhoto(string AttachmentName, byte[] AttachmentBytes)
{
var FileName = AttachmentName;
string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(dirPath, FileName);
FileInfo fi = new FileInfo(filename);
if (!NSFileManager.DefaultManager.FileExists(filename))
{
Stream stream = new MemoryStream(AttachmentBytes);
NSData imgData = NSData.FromStream(stream);
NSError err;
imgData.Save(filename, false, out err);
}
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
QLPreviewController previewController = new QLPreviewController();
previewController.DataSource = new PDFPreviewControllerDataSource(fi.FullName, fi.Name);
UINavigationController controller = FindNavigationController();
if (controller != null)
controller.PresentViewController(previewController, true, null);
});
}
private UINavigationController FindNavigationController()
{
foreach (var window in UIApplication.SharedApplication.Windows)
{
if (window.RootViewController.NavigationController != null)
return window.RootViewController.NavigationController;
else
{
UINavigationController val = CheckSubs(window.RootViewController.ChildViewControllers);
if (val != null)
return val;
}
}
return null;
}
private UINavigationController CheckSubs(UIViewController[] controllers)
{
foreach (var controller in controllers)
{
if (controller.NavigationController != null)
return controller.NavigationController;
else
{
UINavigationController val = CheckSubs(controller.ChildViewControllers);
if (val != null)
return val;
}
}
return null;
}
public string ImageExists(string Filename, byte[] Bytedata)
{
string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(dirPath, Filename);
FileInfo fi = new FileInfo(filename);
if (!NSFileManager.DefaultManager.FileExists(filename))
{
Stream stream = new MemoryStream(Bytedata);
NSData imgData = NSData.FromStream(stream);
NSError err;
imgData.Save(filename, false, out err);
return filename;
}
else
{
return filename;
}
}
}
public class PDFItem : QLPreviewItem
{
string title;
string uri;
public PDFItem(string title, string uri)
{
this.title = title;
this.uri = uri;
}
public override string ItemTitle
{
get { return title; }
}
public override NSUrl ItemUrl
{
get { return NSUrl.FromFilename(uri); }
}
}
public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
string url = "";
string filename = "";
public PDFPreviewControllerDataSource(string url, string filename)
{
this.url = url;
this.filename = filename;
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
return (IQLPreviewItem)new PDFItem(filename, url);
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return 1;
}
}
}
Usage :
IDataViewer dataViewer = DependencyService.Get<IDataViewer>();
dataViewer.showPhoto(FileName, AttachmentBytes);
I am getting the error "Exception thrown: 'System.NotSupportedException' in mscorlib.ni.dll Memory stream is not expandable" when trying to serialize and save an instance of a custom class object.
Here are my saving/loading methods:
public void SerializeObject<T>(T serializableObject, string fileName)
{
if (serializableObject == null) { return; }
try
{
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (MemoryStream stream = new MemoryStream())
{
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
MemoryStream fileNameStream = new MemoryStream(byteArray);
serializer.Serialize(stream, serializableObject);
stream.Position = 0;
xmlDocument.Load(stream);
xmlDocument.Save(fileNameStream);
stream.Dispose();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Debug.WriteLine(TAG + serializableObject.ToString() + " saved");
}
public T DeSerializeObject<T>(string fileName)
{
if (string.IsNullOrEmpty(fileName)) { return default(T); }
T objectOut = default(T);
try
{
string attributeXml = string.Empty;
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
MemoryStream stream = new MemoryStream(byteArray);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(stream);
string xmlString = xmlDocument.OuterXml;
using (StringReader read = new StringReader(xmlString))
{
Type outType = typeof(T);
XmlSerializer serializer = new XmlSerializer(outType);
using (XmlReader reader = XmlReader.Create(read))
{
objectOut = (T)serializer.Deserialize(reader);
reader.Dispose();
}
read.Dispose();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
if (objectOut != null) Debug.WriteLine(TAG + objectOut.ToString() + " loaded");
return objectOut;
}
And here is the object class that I'm trying to save:
public class EntryDataType
{
readonly string TAG = "EntryDataType: ";
private static int idCounter = -1;
public int id;
private EntryDataType parentEdt;
public EntryDataType parentEdtProperty
{
get { return parentEdt; }
set { parentEdt = value; }
}
// row control is not serializable, so ignore it when saving
[XmlIgnore]
public RowControl linkedRowControl;
public int indent = -1;
public int index = -1;
public int linearIndex = -1;
private bool completed = false;
public bool completedProperty {
get { return completed; }
set
{
// set hidden state and set all children's hidden state, i.e. they will do the same
completed = value;
foreach (var item in childList)
{
item.linkedRowControl.SetCompleted(value);
}
}
}
public ChildList<EntryDataType> childList;
public bool bulletButtonChecked;
public string textboxText;
public EntryDataType()
{
// assign unique id to each entry
id = idCounter;
idCounter++;
//Debug.WriteLine(TAG + "new entry " + id + " created");
childList = new ChildList<EntryDataType>();
childList.parentEdtOfChildListProperty = this;
}
}
I've already rewritten the class to eliminate it's constructor's parameters, and to ignore the unserializeable RowControl member. I am just learning .NET and c# so don't fully know what I'm doing yet; any help is greatly appreciated. Thanks :)
OK, I think I see what you are trying to do - serialize and deserialize an object to/from a file. Your way is a bit complicated, it could be simplified, for example like this:
public static void SerializeObject<T>(T serializableObject, string fileName)
{
if (serializableObject == null) { return; }
try
{
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (Stream stream = File.Open(fileName, FileMode.Create))
{
serializer.Serialize(stream, serializableObject);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
public static T DeSerializeObject<T>(string fileName)
{
if (string.IsNullOrEmpty(fileName)) { return default(T); }
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (Stream stream = File.Open(fileName, FileMode.Open))
{
return (T)serializer.Deserialize(stream);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return default(T);
}
There is no need to read/write to a MemoryStream first. You can serialize and deserialize straight from the file.
Also, when using using, there is no need to dispose the object (like your stream.Dispose(); line) - that's what the dispose is for (with the added bonus that if there's an exception, the object will be disposed as well).
I haven't tried this with your class but it should work fine. Give it a try and see if it works.
byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
MemoryStream fileNameStream = new MemoryStream();
...
xmlDocument.Save(fileNameStream);
How is it possible to extend the following code to be executed in a seperate thread?
The methods calling save/load:
private void Save()
{
ClassDiagram diagram = new ClassDiagram();
diagram.ClassDatas = ClassDatas.ToList();
diagram.Connectors = Connectors.ToList();
String filePathAndName = GetSaveFilePathAndName();
saveLoadSerialization.Save(filePathAndName, diagram);
}
private void Open()
{
String filePathAndName = GetOpenFilePathAndName();
ClassDiagram diagram = saveLoadSerialization.Load<ClassDiagram>(filePathAndName);
ClassDatas = new ObservableCollection<ClassData>(diagram.ClassDatas);
Connectors = new ObservableCollection<Connector>(diagram.Connectors);
Connectors.ToList().ForEach(c => { c.From = ClassDatas.Single(cd => cd.Number == c.FromID); c.To = ClassDatas.Single(cd => cd.Number == c.ToID); });
RaisePropertyChanged("ClassDatas");
RaisePropertyChanged("Connectors");
}
The Save/Load XMLSerializer code looks like this - is the best approach an asynchronous thread?
public void Save<T>(String filePathAndName, T data)
{
try
{
using (FileStream file = File.Create(filePathAndName))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(file, data);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public T Load<T>(string filePathAndName)
{
try
{
using (FileStream file = File.OpenRead(filePathAndName))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(file);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return default(T);
}
I have this method I created :
public static bool DeleteFile(FileInfo fileInfo)
{
try
{
fileInfo.Delete();
return true;
}
catch (Exception exception)
{
LogManager.LogError(exception);
return false;
}
}
Now I wrote the following unittest:
[TestMethod]
public void DeleteFileSuccessFul()
{
string fileName = "c:\\Temp\\UnitTest3.txt";
FileInfo fileInfo = new FileInfo(fileName);
File.Create(Path.Combine(fileName));
bool success = FileActions.DeleteFile(fileInfo);
Assert.IsTrue(success);
}
This test fails because the file is in use by a different proces.
The test fails on het bool success = FileActions.DeleteFile(fileInfo); because the file is in use by a different process.
How can I change my test so it works ?
You have to call Dispose method on the FileStream object returned by the File.Create method to release the handle to that file:
[TestMethod]
public void DeleteFileSuccessFul()
{
string fileName = "c:\\Temp\\UnitTest3.txt";
FileInfo fileInfo = new FileInfo(fileName);
using (File.Create(Path.Combine(fileName)))
{
}
bool success = FileActions.DeleteFile(fileInfo);
Assert.IsTrue(success);
}
UPDATE: using block provides a convenient syntax that ensures the Dispose method of an IDisposable object is get called after leaving the scope of the block even if an exception occurs. The equivalent to the above code could be re-written with try-finally block:
[TestMethod]
public void DeleteFileSuccessFul()
{
string fileName = "c:\\Temp\\UnitTest3.txt";
FileInfo fileInfo = new FileInfo(fileName);
FileStream fileStream = null;
try
{
fileStream = File.Create(Path.Combine(fileName));
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
bool success = FileActions.DeleteFile(fileInfo);
Assert.IsTrue(success);
}