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.
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.
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.
File.ReadAllBytes is available. No need for platform dependencies anymore