[英]Convert File To Byte Array Given Its Path In Local Storage?
The following collection contains image paths in the local storage 以下集合包含本地存储中的映像路径
public ObservableCollection<string> Paths;
These file paths are selected by the user and on the Xamarin Android Player they appear like this: 这些文件路径由用户选择,并在Xamarin Android Player上显示如下:
/storage/emulated/0/Download/img1.jpg
/storage/emulated/0/Download/img2.jpg
Now the problem is the following - I want to convert these selected images (based on their paths) to byte arrays for uploading them to server. 现在问题出在下面-我想将这些选定的图像(基于它们的路径)转换为字节数组,以将其上传到服务器。 In WPF/ ASP.Net this would be easily accomplished by the following code: 在WPF / ASP.Net中,可以通过以下代码轻松实现:
byte[] Convert = File.ReadAllBytes("somepath...");
However it appears that there is no such thing available in Xamarin.Forms. 但是,似乎Xamarin.Forms中没有可用的东西。
What functions do I need to use in order to in Xamarin.Forms convert image, based on its physical path in local storage to byte array? 为了在Xamarin.Forms中根据本地存储中的物理路径将图像转换为字节数组,我需要使用哪些函数?
- - - - - -- - - - -- - - - - -- - - - -- - - - - -- - - - - -- - - - - - - - - -- - - - -- - - - - -- - - - -- - - - - -- - - - - -- - - - ------------------------------------------ -----------------------
UPDATE UPDATE
@asp_net Trying to implement your solution: @asp_net尝试实施您的解决方案:
In my PCL project I've added the following two files: 在我的PCL项目中,我添加了以下两个文件:
public interface IFileSystem
{
byte[] ReadAllByteS(string path);
}
and 和
public static class FileUtility
{
public static IFileSystem FileSystem { get; set; }
public static void SetUp(IFileSystem fs)
{
FileSystem = fs;
}
}
On Android I've added the following file: 在Android上,我添加了以下文件:
class SharedFileSytem : IFileSystem
{
public byte[] ReadAllByteS(string path)
{
return File.ReadAllBytes(path);
}
}
And I'm kinda lost at the initialization/registering part - I'm pretty sure I need to add something in my MainActivity.cs file. 而且我在初始化/注册部分有些迷路-我很确定我需要在MainActivity.cs文件中添加一些内容 。
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
//?
LoadApplication(new App());
Because currently, when I'm trying to execute the following code in my PCL code 因为当前,当我尝试在PCL代码中执行以下代码时
FileUtility.FileSystem.ReadAllByteS(somepath);
I get the following error message: 我收到以下错误消息:
"object reference not set to an instance of an object"
To clarify things: System.IO is not available in Portable Class Libraries, which you seem to use. 要澄清的事情:在您似乎使用的可移植类库中,System.IO不可用。 But it is of course available "in Xamarin.Forms". 但是它当然可以在“ Xamarin.Forms”中使用。
Options: 选项:
Example 例
Define an interface within your PCL. 在PCL中定义一个接口。
public interface IFileSystem
{
byte[] ReadAllByteS(string path);
}
Implement it on each platform (or within a shared project for all platforms in this case). 在每个平台上(在这种情况下,在所有平台的共享项目中)实施它。
// Could also be "IosFileSystem" or "SharedFileSytem"
public class DroidFileSystem : IFileSystem
{
public byte[] ReadAllbytes(string path)
{
return File.ReadAllBytes(path);
}
}
Register your implementation when your app starts (there are libraries out there helping you with that). 在您的应用启动时注册您的实现(那里有库可以帮助您)。
public static class Utils
{
public IFileSystem FileSystem { get; set; }
public static void SetUp(IFileSystem fs)
{
FileSystem = fs;
}
}
( Utils
lives in your PCL but its SetUp method is being called from AppDelegate or your Main Activity.) ( Utils
存在于您的PCL中,但正在从AppDelegate或您的Main Activity中调用其SetUp方法。)
Use it: 用它:
Utils.FileSystem.ReadAllBytes(...)
You can load image into Memory stream and convert in to array,Something like this should work 您可以将图像加载到内存流中并转换为数组,这样的事情应该可以工作
using (var streamReader = new StreamReader(path))
{
using (var memoryStream = new MemoryStream())
{
streamReader.BaseStream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
public async static Task<IFile> AsStorageFileAsync(this byte[] byteArray, string fileNameEx)
{
IFile file = await StorageData.LocalStorage().CreateFileAsync(fileNameEx, CreationCollisionOption.ReplaceExisting);
using (var fileHandler = await file.OpenAsync(FileAccess.ReadAndWrite))
{
byte[] dataBuffer = byteArray;
await fileHandler.WriteAsync(dataBuffer, 0, dataBuffer.Length);
}
return file;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.