UWP picks folders and reads pictures from them

Source: Internet
Author: User

The UWP has a big difference in the way files are manipulated, wpf,winform, and so on, mainly because the UWP has a strict need for permissions, and cannot read files as freely as they once did.

1. First find package.appxmanifest This file, in the function check the required functions, add in the declaration, after this can be relieved to write code.

2. Open the file selector, select the folder, and save the selected folder.

            //Open File SelectorFolderpicker pick =NewFolderpicker (); Pick. Filetypefilter.add (". PNG"); Pick. Filetypefilter.add (". jpg"); Pick. Filetypefilter.add (". bmp"); Iasyncoperation<StorageFolder> Foldertask =pick.            Picksinglefolderasync (); Storagefolder folder=awaitFoldertask; //var folder = await pick. Picksinglefolderasync ();Storagefolder Folder =NULL; stringAddress; stringToken =""; if(Folder! =NULL) {Folder=folder; Address=folder.                Path; Token=StorageApplicationPermissions.FutureAccessList.Add (folder);            } StorageApplicationPermissions.FutureAccessList.GetFolderAsync (Token); //Get local FolderStoragefolder folderlocal =ApplicationData.Current.LocalFolder; //Create a folder account            Try{folderlocal=awaitFolderlocal.getfolderasync (FOLDERSTR); }            Catch(filenotfoundexception) {folderlocal=awaitFolderlocal.createfolderasync (FOLDERSTR); } StorageFile File=awaitFolderlocal.createfileasync (Folderstr+". JSON", creationcollisionoption.replaceexisting); //Save the selected folder token            varJSON =jsonserializer.create (); ImagePath ImagePath=NewImagePath {Id = DateTime.Now.ToString ("Yymmddhhmmss"), Path =Token}; stringImagejson =imagepath.stringify (); if(File! =NULL)            {                Try                {                    using(Storagestreamtransaction transaction =awaitfile. Opentransactedwriteasync ()) {using(DataWriter DataWriter =NewDataWriter (transaction.                             Stream) {Datawriter.writeint32 (Encoding.UTF8.GetByteCount (Imagejson));                            Datawriter.writestring (Imagejson); Transaction. Stream.size=awaitDatawriter.storeasync (); awaitTransaction.commitasync (); }                    }                }                Catch(Exception ex) {Throwex; }            }            

3. Get the picture under the selected folder

StorageFile filelocal =awaitStoragefile.getfilefromapplicationuriasync (NewUri ("ms-appdata:///local/account/"+ Imagehelper.folderstr +". JSON")); if(Filelocal! =NULL)            {                Try                {                    //reads local file contents, and deserializes                    using(Irandomaccessstream Readstream =awaitFilelocal.openasync (Fileaccessmode.read)) {                        using(DataReader DataReader =NewDataReader (Readstream)) {UInt64 size=readstream.size; if(Size <=uint32.maxvalue) {awaitDatareader.loadasync (sizeof(Int32)); Int32 stringsize=Datareader.readint32 (); awaitDatareader.loadasync ((UInt32) stringsize); stringFilecontent = Datareader.readstring ((UINT) stringsize); ImagePath ImagePath=NewImagePath (filecontent); Storagefolder folder=awaitStorageApplicationPermissions.FutureAccessList.GetFolderAsync (Imagepath.path); //Filter Pictures                                varQueryoptions =NewWindows.Storage.Search.QueryOptions (); QUERYOPTIONS.FILETYPEFILTER.ADD (". PNG"); QUERYOPTIONS.FILETYPEFILTER.ADD (". jpg"); QUERYOPTIONS.FILETYPEFILTER.ADD (". bmp"); varquery =folder.                                Createfilequerywithoptions (queryoptions); varFiles =awaitquery.                                Getfilesasync ();                                ImagePath img; Imglist=NewObservablecollection<imagepath>(); foreach(varIteminchfiles) {Irandomaccessstream IRandom=awaititem.                                                                       OpenAsync (Fileaccessmode.read); //using a stream source for an image sourceBitmapImage BitmapImage =NewBitmapImage (); Bitmapimage.decodepixelwidth= the; Bitmapimage.decodepixelheight= -; awaitBitmapimage.setsourceasync (irandom); IMG=NewImagePath (); Img. Path=item.                                    Path; Img. File=BitmapImage; Img. Storage=item;                                Imglist.add (IMG); } Imageview.itemssource=imglist; }                        }                    }                }                Catch(Exception exce) {await NewMessagedialog (exce. ToString ()).                    Showasync (); ThrowExce; }            }

The results of the final implementation are probably as follows:

  

UWP picks folders and reads pictures from them

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.