1

currently I am saving image from camera in gallery and its path in sq lite table.

I need byte array of this saved image in during API call.

so can I get byte array of image by its name?

please help me to.

1
  • I don't think it can be done directly in Xamarin.Forms with dependency service you can convert the file var byteArray = File.ReadAllBytes(imagePath); Commented Apr 19, 2017 at 8:50

1 Answer 1

2

If you have the file path you should just be able to get it by File.ReadAllBytes(imagePath);.

However, this is not available in a generic matter within Xamarin.Forms. You will need to use the DependencyService for it.

It could look like this:

Define an interface in your shared code

public interface ILocalFileProvider
{
    byte[] GetFileBytes(string filePath);
}

Then implement it in the platform projects, like this

public class LocalFileProvider_iOS : ILocalFileProvider
{
    public byte[] GetFileBytes(string filePath)
    {
        return File.ReadAllBytes(filePath);
    }
}

It will look pretty much the same in Android, but of course you should handle errors, exceptions etc. Also don't forget to add the [assembly: Dependency(typeof(LocalFileProvider_iOS))] above the namespace.

You can now use it in your shared code like this: var bytes = DependencyService.Get<ILocalFileProvider>().GetFileBytes(file.Path);

Also, here it needs some finetuning.

What you are doing now is calling upon generic interface from your shared code. Runtime it is determined which implementation the interface gets and that way the platform specific code is implemented as the interface.

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

1 Comment

if the code lives on .net standard, File.ReadAllBytes is available. No need for platform dependencies anymore

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.