uwp选取文件夹并读取其中的图片
生活随笔
收集整理的這篇文章主要介紹了
uwp选取文件夹并读取其中的图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
uwp對文件的操作和wpf,winform等等有很大的不同,主要原因是uwp對權限的要求比較嚴格,不能想從前那樣隨心所欲的讀取文件。
1.首先找到Package.appxmanifest這個文件,在功能里面勾選需要的功能,在申明里添加,在此之后才能安心寫代碼。
?
2.打開文件選擇器,選擇文件夾,并保存選擇的文件夾。
//打開文件選擇器FolderPicker pick = new FolderPicker();pick.FileTypeFilter.Add(".png");pick.FileTypeFilter.Add(".jpg");pick.FileTypeFilter.Add(".bmp");IAsyncOperation<StorageFolder> folderTask = pick.PickSingleFolderAsync();StorageFolder folder = await folderTask;//var folder = await pick.PickSingleFolderAsync();StorageFolder Folder = null;string Address;string Token = "";if (folder != null){Folder = folder;Address = folder.Path;Token = StorageApplicationPermissions.FutureAccessList.Add(folder);}StorageApplicationPermissions.FutureAccessList.GetFolderAsync(Token);//獲取本地文件夾StorageFolder folderLocal = ApplicationData.Current.LocalFolder;//創建一個文件夾accounttry{folderLocal = await folderLocal.GetFolderAsync(folderStr);}catch (FileNotFoundException){folderLocal = await folderLocal.CreateFolderAsync(folderStr);}StorageFile file = await folderLocal.CreateFileAsync( folderStr + ".json", CreationCollisionOption.ReplaceExisting);//保存選擇的文件夾Tokenvar json = JsonSerializer.Create();ImagePath imagePath = new ImagePath { Id = DateTime.Now.ToString("yyMMddHHmmss"), Path = Token };string imageJson = imagePath.Stringify();if (file != null){try{using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync()){using (DataWriter dataWriter = new DataWriter(transaction.Stream)){dataWriter.WriteInt32(Encoding.UTF8.GetByteCount(imageJson));dataWriter.WriteString(imageJson);transaction.Stream.Size = await dataWriter.StoreAsync();await transaction.CommitAsync();}}}catch (Exception ex){throw ex;}}3.獲取已選擇文件夾下的圖片
StorageFile fileLocal = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/account/" + ImageHelper.folderStr + ".json"));if (fileLocal != null){try{//讀取本地文件內容,并且反序列化using (IRandomAccessStream readStream = await fileLocal.OpenAsync(FileAccessMode.Read)){using (DataReader dataReader = new DataReader(readStream)){UInt64 size = readStream.Size;if (size <= UInt32.MaxValue){await dataReader.LoadAsync(sizeof(Int32));Int32 stringSize = dataReader.ReadInt32();await dataReader.LoadAsync((UInt32)stringSize);string fileContent = dataReader.ReadString((uint)stringSize);ImagePath imagePath = new ImagePath(fileContent);StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(imagePath.Path);//篩選圖片var queryOptions = new Windows.Storage.Search.QueryOptions();queryOptions.FileTypeFilter.Add(".png");queryOptions.FileTypeFilter.Add(".jpg");queryOptions.FileTypeFilter.Add(".bmp");var query = folder.CreateFileQueryWithOptions(queryOptions);var files = await query.GetFilesAsync();ImagePath img;imgList = new ObservableCollection<ImagePath>();foreach (var item in files){IRandomAccessStream irandom = await item.OpenAsync(FileAccessMode.Read);//對圖像源使用流源BitmapImage bitmapImage = new BitmapImage();bitmapImage.DecodePixelWidth = 160;bitmapImage.DecodePixelHeight = 100;await bitmapImage.SetSourceAsync(irandom);img = new ImagePath();img.Path = item.Path;img.File = bitmapImage;img.Storage = item;imgList.Add(img);}imageView.ItemsSource = imgList;}}}}catch (Exception exce){await new MessageDialog(exce.ToString()).ShowAsync();throw exce;}}最后的實現顯現效果大概如下:
/*----------------------------------------------更新----------------------------------------------*/
謝謝yinyue200?的提醒,Package.appxmanifest可以不用配置。
?
轉載于:https://www.cnblogs.com/bestckk/p/6035139.html
總結
以上是生活随笔為你收集整理的uwp选取文件夹并读取其中的图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二章 centos安装maven
- 下一篇: 漫水填充