PCLStorage e dados binários
-
21-12-2019 - |
Pergunta
Sou novo nessas bibliotecas PCL, estou desenvolvendo um aplicativo para iPhone com Xamarin e não consigo encontrar uma maneira de salvá-lo no telefone.O mais próximo que chego é com o PCLStorage mas ele só salva texto.Existe outra maneira de salvar arquivos binários com outro procedimento.Obrigado.
foreach (images element in json_object)
{
//var nameFile = Path.Combine (directoryname, element.name);
try{
IFile file = await folder_new.GetFileAsync(element.name);
}catch(FileNotFoundException ex ){
RestClient _Client = new RestClient(element.root);
RestRequest request_file = new RestRequest("/images/{FileName}");
request_file.AddParameter("FileName", element.name, ParameterType.UrlSegment);
_Client.ExecuteAsync<MemoryStream>(
request_file,
async Response =>
{
if (Response != null)
{
IFolder rootFolder_new = FileSystem.Current.LocalStorage;
IFile file_new = await rootFolder_new.CreateFileAsync(element.name,CreationCollisionOption.ReplaceExisting);
await file_new.WriteAllTextAsync(Response.Data);
}
});
}
}
Solução
Use o método IFile.OpenAsync para obter um fluxo que você pode usar para ler/gravar dados binários.Veja como você leria um arquivo:
IFile file = await folder_new.GetFileAsync(element.name);
using (Stream stream = await file.OpenAsync(FileAccess.Read))
{
// Read stream and process binary data from it...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow