2016-09-28 37 views
0

我需要從Plugin.Media.Abstractions.MediaFile類型轉換爲Windows.Storage.StorageFileUWP - 將MediaFile轉換爲StorageFile

原因是我得到一個MediaFile,我想通過該路徑上傳到服務器,但在UWP中,您必須使用StorageFiles而不是文件路徑,因爲否則UWP不知道用戶擁有這個文件的權限,只是拒絕訪問。所以我想將MediaFile轉換爲StorageFile,以便能夠在用戶的臨時文件夾中創建文件的副本。該文件夾中的文件可以通過文件路徑訪問。

那就是我已經試過:

Stream stream = _mediaFile.GetStream(); 
StreamReader reader = new StreamReader(stream, Encoding.UTF8); 
string streamText = reader.ReadToEnd(); 

_websocketManager.sendFileMessage(_selectedChat.chatId, _nachrichttext, streamText); 

sendFileMessage()從接口的方法,因此參數必須是一個字符串,我不能改變的。本來這個字符串應該是路徑,所以對我來說這是一個便宜的解決方法,但是這對我想要達到的目標是合法的。

在WebsocketManager,這是我的方法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(); 

而且,正如我在代碼註釋,我需要找出哪些數據類型MediaFile爲(巴紐,JPG格式,等等) 。

回答

1

您可以參考xamarin官方文檔Working with Files來讀寫文件,我們需要在每個平臺上使用本地文件API。

對於UWP的應用程序,你可以在便攜式項目創建一個接口​​這樣的:

public interface ISaveAndLoad 
{ 
    void SaveImage(string filename, Stream filestream); 

    Task<string> LoadImage(string filename); 
} 

然後在你的UWP應用程序創建一個類繼承,並實現該​​界面是這樣的:

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); 
     } 
    } 
} 

最後,當您選取圖片時,您可以將MediaFile轉換爲StorageFile,並獲取臨時文件的路徑,如下所示:

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 
+0

謝謝。我改了一下你的代碼,以便它適用於我的具體情況。我只需要SaveImage(),然後讓它返回文件的路徑。現在我可以使用這個路徑爲我的方法「sendFileMessage」! –

相關問題