Reputation: 135
I am trying to upload a pdf file using the nuget package filepicker.
I’m able to getting the file name & file local path.
But when I am trying to convert the file to a byte array I am getting an error.
It's my pcl Code-
public interface ILoclFileProvider
{
byte[] GetFileBytes(string path);
}
public static class FileUtility
{
public static ILoclFileProvider FileSystem { get; set; }
public static void SetUp(ILoclFileProvider fs)
{
FileSystem = fs;
}
}
This is my Droid Project code-
public class LocalFileProvider_Droid : ILoclFileProvider
{
public byte[] GetFileBytes(string filePath)
{
return File.ReadAllBytes(filePath);
}
}
and in my pcl project I'm calling this-
var bytes = FileUtility.FileSystem.GetFileBytes(filePath);
and I'm getting an error-
Object reference not set to an instance of an object.
what is wrong in my code?
Upvotes: 1
Views: 697
Reputation: 34013
I think you are calling the DependencyService wrong. Try this:
var bytes = DependencyService.Get<ILoclFileProvider>().GetFileBytes(filePath);
Also make sure you registered it through the right attribute above the namespace in your Android project.
[assembly: Xamarin.Forms.Dependency (typeof (LocalFileProvider_Droid))]
This line makes sure that the dependency is registered within the Xamarin runtime. For more information on the DependencyService, check out these new Microsoft docs pages.
Upvotes: 1