[WP8.1開發]從網路下載圖片直接存入手機照片庫

來源:互聯網
上載者:User

標籤:

開發應用經常要將網路上的圖片儲存下來,在8.1有FileSavePicker可以用,但使用者反映每次都要選擇路徑好麻煩,於是開始尋找直接存到照片庫的方法,首先肯定要從網路上拿到圖片流,然後在圖片庫裡建立一個檔案,把圖片流弄到檔案裡去就大功告成了,各種查MSDN後終於解決,話不多說,上代碼

            string fileName = "picture"+DateTime.Now.Ticks+".jpg";            //在SavePictures檔案夾下產生一個空檔案用於儲存圖片            StorageFile destinationFile = await KnownFolders.SavedPictures.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);                       using(var response =await HttpWebRequest.Create(savePictureInfo.imageUri).GetResponseAsync())            {                //擷取到圖片流                using (Stream responseStream = response.GetResponseStream())                {                    using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))                    {                        using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))                        {                            await RandomAccessStream.CopyAndCloseAsync(responseStream.AsInputStream(), destinationStream);                        }                    }                }                //擷取檔案更新狀態                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(destinationFile);                 if (status == FileUpdateStatus.Complete)                 {                     string SAVE_SUCCESS = loader.GetString("uSaveSuccess");                     await new MessageDialog(SAVE_SUCCESS).ShowAsync();                 }                 else                 {                     string SAVE_FAILED = loader.GetString("uSaveFailed");                     await new MessageDialog(SAVE_FAILED).ShowAsync();                 }    

剛學WP不久,如有錯誤,歡迎指正。

[WP8.1開發]從網路下載圖片直接存入手機照片庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.