我想下载一个图像并将其存储在本地存储中的特定文件夹中.
我用这个来下载图片:
var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename);
var img = ImageSource.FromStream(() => new MemoryStream(imageData));
Run Code Online (Sandbox Code Playgroud)
Mar*_*hel 12
创建FileService接口
在您的共享代码中,创建一个新的接口,例如,名为IFileService.cs
public interface IFileService
{
void SavePicture(string name, Stream data, string location="temp");
}
Run Code Online (Sandbox Code Playgroud)
实施Android
在你的android项目中,创建一个名为"Fileservice.cs"的新类.
确保它派生自之前创建的界面,并使用依赖关系信息进行装饰:
[assembly: Dependency(typeof(FileService))]
namespace MyApp.Droid
{
public class FileService : IFileService
{
public void SavePicture(string name, Stream data, string location = "temp")
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", location);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
实现iOS iOS 的实现基本相同:
[assembly: Dependency(typeof(FileService))]
namespace MyApp.iOS
{
public class FileService: IFileService
{
public void SavePicture(string name, Stream data, string location = "temp")
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", location);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要保存文件,请在共享代码中调用
DependencyService.Get<IFileService>().SavePicture("ImageName.jpg", imageData, "imagesFolder");
Run Code Online (Sandbox Code Playgroud)
应该很高兴去.