1

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?

1
  • 1
    You need to use DependencyService. Commented Mar 27, 2018 at 6:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes it worked. but now am getting an error that 'Access to the path "/storage/emulated/0/Download/future_of_js_2018_progress.pdf" is denied'. do you know what should i do here?
Yes. Read_External_Storage and Write_External_Storage is checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.