Reputation: 2422
I need to convert from typePlugin.Media.Abstractions.MediaFile
to Windows.Storage.StorageFile
.
Reason for is that I'm getting a MediaFile
which I want to upload to a server via the path, but in UWP you must work with StorageFiles instead of file paths because else UWP doesn't know that the user has the permission for this file and just denies the access. So I want to convert the MediaFile to a StorageFile to be able to create a copy of the file in the temporary folder of the user. Files in this folder can be accessed by the file's path.
That's what I've tried:
Stream stream = _mediaFile.GetStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string streamText = reader.ReadToEnd();
_websocketManager.sendFileMessage(_selectedChat.chatId, _nachrichttext, streamText);
sendFileMessage()
is a method from an Interface so the parameter has to be a string and I can't change that. Originally this string is supposed to be the path, so it's a cheap workaround by me but that's legit for what I'm trying to achieve.
In the WebsocketManager, this is my method sendFileMessage()
:
byte[] byteArray = Encoding.UTF8.GetBytes(streamText);
MemoryStream stream = new MemoryStream(byteArray);
StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
var temporaryFile = await temporaryFolder.CreateFileAsync("temporaryFile", CreationCollisionOption.ReplaceExisting);
using (var fileStream = File.Create(temporaryFolder.Path + "\\TemporaryFile")) // TODO: How to data type? (.png, .jpg, ...)
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
string filePath = temporaryFile.Path;
string remoteFilePath = null;
if (!string.IsNullOrEmpty(filePath))
{
await StorageFile.GetFileFromPathAsync(filePath);
remoteFilePath = await uploadFile(filePath);
}
dataWriter.WriteString(WebsocketRequestFactory.Create(SocketEventsEnm.MESSAGE_OUT, chatId, message, remoteFilePath, TypeEnm.IMAGE));
await SendData(dataWriter);
await temporaryFile.DeleteAsync();
Also, as I commented in the code, I need to find out which data type the MediaFile
was (.png, .jpg, etc).
Upvotes: 0
Views: 561
Reputation: 16652
You can refer to the xamarin official document Working with Files, to read and write files, we need to use the native file APIs on each platform.
For UWP apps, you can create an interface ISaveAndLoad
in portable project like this:
public interface ISaveAndLoad
{
void SaveImage(string filename, Stream filestream);
Task<string> LoadImage(string filename);
}
Then in your UWP app create a class inherit and implement this ISaveAndLoad
interface like this:
public class SaveAndLoad_UWP : ISaveAndLoad
{
public async Task<string> LoadImage(string filename)
{
StorageFolder local = ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync(filename);
return file.Path;
}
public async void SaveImage(string filename, Stream filestream)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile File = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
using (var stream = await File.OpenStreamForWriteAsync())
{
await filestream.CopyToAsync(stream);
}
}
}
At last, when you pick a image, you can convert the MediaFile
to StorageFile
and get the temporary file's path like this:
var file = await CrossMedia.Current.PickPhotoAsync(); //pick a file
if (file == null)
return;
var path = file.Path;
var filetype = path.Split('.').Last(); //get file type
var filestream = file.GetStream();
DependencyService.Get<ISaveAndLoad>().SaveImage("temp." + filetype, filestream); //save file
var temppath = DependencyService.Get<ISaveAndLoad>().LoadImage("temp." + filetype); //get temp file's path
Upvotes: 1