Exemplo n.º 1
0
		async void loginClick ()
		{
			try 
			{
				AsyncRequest asyncrequest = new AsyncRequest ()
					.SetUrl ("http://hdwallpaperd.com/wp-content/uploads/1685318.jpg");

				var result = await asyncrequest.ExecuteTask();
				//				Encoding.UTF8.GetString(result);
				//				await BitmapFactory.DecodeByteArrayAsync (source, 0, source.Length);

				//imagePicture.Source = "http://hdwallpaperd.com/wp-content/uploads/1685318.jpg";

				await FileHelper.WriteToFile("test.jpg", result);
				imagePicture.Source = ImageSource.FromFile(await FileHelper.GetFilePath("test.jpg"));                
			} 
			catch (Exception ex) 
			{
				Debug.WriteLine ("[ProfilePage][loginClick] Error {0}", ex.Message);
			}
		}
        /// <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;
            }
        }