コード例 #1
0
        /// <summary>
        /// 	Download the Images.
        /// </summary>
        /// <returns>The download.</returns>
        /// <param name="url">URL.</param>
        public async Task<CacheModel> ImageDownload(string url)
        {
            try
            {
                if (url == null) return null;

                SetData();

                var filename = url.Substring(url.LastIndexOf("/") + 1);

                if (GetCache(url) != null) return GetCache(url);
                if (await FileHelper.ValidateFile(filename) && GetCache(url) == null)
                {
                    CacheModel cache = new CacheModel(url, await FileHelper.GetFilePath("thumbnail_" + filename), await FileHelper.GetFilePath(filename));
                    Instance._cache.Add(cache);
                    return cache;
                }

                try
                {
                    AddTaskIndex(url);
                    RunningAsyncTasks runningAsyncTask = GetRunningAsyncTask(url);
                    await runningAsyncTask._asyncCache;
                    RemoveTaskIndex(url);
                    return GetCache(url);
                }
                catch (NullReferenceException) { }

                AddTaskIndex(url);
                AsyncRequest downloadimage = new AsyncRequest().SetUrl(url);
                RunningAsyncTasks running = new RunningAsyncTasks(downloadimage, SetCache(url, filename, downloadimage.ExecuteTask()));
                Instance._requests.Add(running);
                return await running._asyncCache;
            }
#if __ANDROID__
			catch (OutOfMemoryError ex) 
			{
				Debug.WriteLine ("[AsyncStructure][ImageDownload] Out of Memory Error: " + ex.Message + " on this URL: " + url);
				return null;
			}
			catch (NullPointerException ex)
			{
				Debug.WriteLine ("[AsyncStructure][ImageDownload] Null Pointer Exception Error: " + ex.Message + " on this URL: " + url);
				return null;
			}
#endif
#if __IOS__
#endif
#if WINDOWS_PHONE
            catch (System.IO.FileNotFoundException ex)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] File Not Found Exception Error: " + ex.Message + " on this URL: " + url);
                return null;
            }
            catch (System.InvalidOperationException ex)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] Invalid Operation Exception Error: " + ex.Message + " on this URL: " + url);
                return null;
            }

#endif
            catch (OutOfMemoryException exmem)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] Out of memory Error: " + exmem.Message + " on this URL: " + url);
                return null;
            }
            catch (NullReferenceException exmem)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] Null reference Error: " + exmem.Message + " on this URL: " + url);
                return null;
            }
            catch (PlatformNotSupportedException exmem)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] Platform not supported Error: " + exmem.Message + " on this URL: " + url);
                return null;
            }
            catch (System.Exception exmem)
            {
                Debug.WriteLine("[AsyncStructure][ImageDownload] System Exception Error: " + exmem.Message + " on this URL: " + url);
                return null;
            }
        }
コード例 #2
0
        /// <summary>
        ///     Sets Files and cache model
        /// </summary>
        /// <param name="url">URL of the HTTP request</param>
        /// <param name="filename">name of the file to be generated</param>
        /// <param name="imagedownloaded">array of bytes</param>
        /// <returns>Object of the CacheModel class, null if there aren't any</returns>
        private async Task<CacheModel> SetCache(string url, string filename, Task<byte[]> imagedownloaded)
        {
            var image = await imagedownloaded;
            if (image != null)
            {
				var imagedownloadedresized = await ImageResizer.ResizeImage(image, 50, 50);
                await FileHelper.WriteToFile(filename, image);
                await FileHelper.WriteToFile("thumbnail_" + filename, imagedownloadedresized);
                CacheModel cache = new CacheModel(url, await FileHelper.GetFilePath("thumbnail_" + filename), await FileHelper.GetFilePath(filename));
                Instance._cache.Add(cache);
                RemoveTaskIndex(url);
                return cache;
            }

            RemoveTaskIndex(url);
            return null;
        }