从JSON本地存储图像。如何?

时间:2015-10-06 07:41:44

标签: c# json image windows-phone-8 local-storage

不知道你能不能帮助我...

我从API中检索了一些数据,现在,在反序列化之后我将它放在列表中......

现在,这个数据有一些我需要放在不同页面的gridview中的图像,因为我被要求在本地保存数据(并偶尔检查更新)我想知道我该怎么做必须做到:

  1. 确保我得到图片(而不仅仅是名字)
  2. 将其保存到本地存储(带有其ID),以便稍后创建 数据绑定资源......
  3. 这就是我所拥有的:

    public class GetFromJson
    {
    
        private Medio medio = new Medio();
        public async void GetFullResponse(string address)
        {
            try
            {
                var client = new HttpClient();
    
                var uri = new Uri(address);
                var Response = await client.GetAsync(uri);
                var statusCode = Response.StatusCode;
    
                Response.EnsureSuccessStatusCode();
    
                var ResponseText = await Response.Content.ReadAsStringAsync();
    
                string myJson = ResponseText;
    
                medio.deserializeAndConvert(myJson);
    
            }
    
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
    }
    

    然后,反序列化数据:

    public class Medio
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string img { get; set; }
        public string medios_id { get; set; }
    
        public void deserializeAndConvert(string aaa)
        {
            JObject myGeneral = JObject.Parse(aaa);
            IList<JToken> results = myGeneral["resp"]["medios"].Children().ToList();
    
            IList<Medio> searchResults = new List<Medio>();
            foreach (JToken result in results)
            {
                Medio searchResult = JsonConvert.DeserializeObject<Medio>(result.ToString());
                searchResults.Add(searchResult);
                Debug.WriteLine(searchResults);
    
            }
    
    
            Debug.WriteLine(searchResults.Count);
            GlobalVariables.Medios = searchResults;
        }
    }
    

    有了这个,我得到了我需要处理的数据......并且在应用程序的开始时加载了。

    在那之后,当我们转到我必须显示图像的页面时,我有这个:

    public BankAndCards()
        {
    
            createGridView();
            this.InitializeComponent();
        }
    
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    
        void createGridView()
        {
    
            QueryPicturesToShow();
        }
    
        public void QueryPicturesToShow()
        {
            var pics = from medio in GlobalVariables.Medios
                             where medio.id != null
                             select new { Name = medio.name, Id = medio.id, Picture = medio.img};
    
            foreach (var item in pics)
            {
                Debug.WriteLine(item.Name);
                Debug.WriteLine(item.Id);
                Debug.WriteLine(item.Picture);
    
                savePicToDisk();
            }
        }
    
        private async void savePicToDisk()
        {
            //what should I do here?
        }
    }
    

    我从API获得的数据(按顺序:名称,ID,图片)

      

    Banco Galicia 2 /方法/ 21030342

         

    Bango River 3 /方法/ 210321012

    非常感谢!!!

1 个答案:

答案 0 :(得分:0)

我明白了...... 出于某种原因,在我拔掉手机并重新插上电源之前,我无法看到文件夹中的图片......

public void QueryPicturesToShow()
    {

        var pics = from medio in GlobalVariables.Medios
                   where medio.img != null
                   select new { Name = medio.name, Id = medio.id, Picture = medio.img };

        foreach (var item in pics)
        {
            Debug.WriteLine(item.Name);
            Debug.WriteLine(item.Id);
            Debug.WriteLine(item.Picture);



            savePicToDisk(item.Picture, item.Name, item.Id);
        }
    }

                private async void savePicToDisk(string picAddress, string picName, string picId)
                {

                    StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("workingFolder", CreationCollisionOption.OpenIfExists);

                    StorageFile file = await folder.CreateFileAsync((picName + picId + ".png"), CreationCollisionOption.ReplaceExisting);

                    string url = GlobalVariables.apiUrl + picAddress;


                    Debug.WriteLine(url);
                    HttpClient client = new HttpClient();

                    byte[] responseBytes = await client.GetByteArrayAsync(url);

                    var stream = await file.OpenAsync(FileAccessMode.ReadWrite);

                    using (var outputStream = stream.GetOutputStreamAt(0))
                    {
                        DataWriter writer = new DataWriter(outputStream);
                        writer.WriteBytes(responseBytes);
                        await writer.StoreAsync();
                        await outputStream.FlushAsync();

                    }
    }

所以,现在,我只需要知道如何将它们绑定到gridView,所以,如果有人知道,你会节省我很多时间(我想把它们放在列表中,但是我我不确定是否会起作用)

谢谢!