Advertisement
MagnusArias

PUM | Zad 2

Apr 6th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. using System;
  2. using Android.App;
  3. using Android.Widget;
  4. using Android.OS;
  5. using Android.Graphics;
  6.  
  7. using System.IO;
  8. using System.Net;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Zad2PUM
  12. {
  13.  
  14.     [Activity(Label = "Zad2PUM", MainLauncher = true,
  15.         ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
  16.     public class MainActivity : Activity
  17.     {
  18.         int _counter = 0;
  19.         string _imie, _nazwisko;
  20.         WebClient webClient;
  21.         Button downloadButton, clickButton;
  22.         ImageView imageview;
  23.         ProgressBar downloadProgress;
  24.         private EditText Imie, Nazwisko, CounterText, urlText;
  25.         private string urlGlobal;
  26.  
  27.  
  28.         protected override void OnCreate(Bundle bundle)
  29.         {
  30.             base.OnCreate(bundle);
  31.  
  32.             // Set our view from the "main" layout resource
  33.             SetContentView(Resource.Layout.Main);
  34.  
  35.             if (bundle != null)
  36.             {
  37.                 _imie = bundle.GetString("imie", "");
  38.                 _nazwisko = bundle.GetString("nazwisko", "");
  39.                 _counter = bundle.GetInt("counter", 0);
  40.             }
  41.  
  42.             CounterText = FindViewById<EditText>(Resource.Id.CounterText);
  43.             Imie = FindViewById<EditText>(Resource.Id.EditName);
  44.             Nazwisko = FindViewById<EditText>(Resource.Id.EditSecondName);
  45.             urlText = FindViewById<EditText>(Resource.Id.ImageAddress);
  46.  
  47.             this.downloadButton = FindViewById<Button>(Resource.Id.ButtonDowload);
  48.             this.clickButton = FindViewById<Button>(Resource.Id.CounterButton);
  49.             this.imageview = FindViewById<ImageView>(Resource.Id.ImageView);
  50.             this.downloadProgress = FindViewById<ProgressBar>(Resource.Id.ProgressBar);
  51.  
  52.  
  53.             CounterText.Text = _counter.ToString();
  54.             urlText.Text = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Reh11ibb.jpg/240px-Reh11ibb.jpg";
  55.             urlGlobal = urlText.Text;
  56.  
  57.             downloadButton.Click += downloadAsync;
  58.  
  59.             clickButton.Click += (object sender, EventArgs e) =>
  60.             {
  61.                 _counter++;
  62.                 CounterText.Text = _counter.ToString();
  63.             };
  64.         }
  65.  
  66.         async void downloadAsync(object sender, System.EventArgs evarg)
  67.         {
  68.             webClient = new WebClient();
  69.             var url = new Uri(urlGlobal);
  70.             byte[] bytes = null;
  71.  
  72.             webClient.DownloadProgressChanged += HandleDownloadProgressChanged;
  73.  
  74.             this.downloadButton.Text = "Stop";
  75.             this.downloadButton.Click -= downloadAsync;
  76.             this.downloadButton.Click += cancelDownload;
  77.             try
  78.             {
  79.                 bytes = await webClient.DownloadDataTaskAsync(url);
  80.             }
  81.             catch (TaskCanceledException)
  82.             {
  83.                 Toast.MakeText(this, "Przerwano pobieranie", ToastLength.Long).Show();
  84.                 return;
  85.             }
  86.             catch (Exception e)
  87.             {
  88.                 Console.WriteLine(e.ToString());
  89.  
  90.                 renewButton();
  91.                 return;
  92.             }
  93.             string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  94.             string localFilename = "sarna.jpg";
  95.             string localPath = System.IO.Path.Combine(documentsPath, localFilename);
  96.  
  97.             FileStream fs = new FileStream(localPath, FileMode.OpenOrCreate);
  98.             await fs.WriteAsync(bytes, 0, bytes.Length);
  99.  
  100.             fs.Close();
  101.  
  102.             BitmapFactory.Options options = new BitmapFactory.Options();
  103.             options.InJustDecodeBounds = true;
  104.             await BitmapFactory.DecodeFileAsync(localPath, options);
  105.  
  106.             options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / imageview.Height : options.OutWidth / imageview.Width;
  107.             options.InJustDecodeBounds = false;
  108.  
  109.             Bitmap bitmap = await BitmapFactory.DecodeFileAsync(localPath, options);
  110.  
  111.             Toast.MakeText(this, "Pobrano!", ToastLength.Long).Show();
  112.  
  113.             imageview.SetImageBitmap(bitmap);
  114.  
  115.             renewButton();
  116.         }
  117.  
  118.         void HandleDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  119.         {
  120.             this.downloadProgress.Progress = e.ProgressPercentage;
  121.         }
  122.  
  123.         void cancelDownload(object sender, System.EventArgs eeaa)
  124.         {
  125.             Toast.MakeText(this, "Przerwano pobieranie", ToastLength.Long).Show();
  126.             if (webClient != null)
  127.                 webClient.CancelAsync();
  128.  
  129.             webClient.DownloadProgressChanged -= HandleDownloadProgressChanged;
  130.  
  131.             renewButton();
  132.         }
  133.  
  134.         protected override void OnSaveInstanceState(Bundle outState)
  135.         {
  136.             outState.PutInt("counter", _counter);
  137.             outState.PutString("imie", _imie);
  138.             outState.PutString("nazwisko", _nazwisko);
  139.             base.OnSaveInstanceState(outState);
  140.  
  141.         }
  142.  
  143.         protected override void OnRestoreInstanceState(Bundle savedState)
  144.         {
  145.             base.OnRestoreInstanceState(savedState);
  146.             _counter = savedState.GetInt("counter");
  147.             _imie = savedState.GetString("imie");
  148.             _nazwisko = savedState.GetString("nazwisko");
  149.         }
  150.  
  151.         private async Task loadMyImage()
  152.         {
  153.             string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  154.             string localFilename = "sarna.jpg";
  155.             string localPath = System.IO.Path.Combine(documentsPath, localFilename);
  156.  
  157.             BitmapFactory.Options options = new BitmapFactory.Options();
  158.             options.InJustDecodeBounds = true;
  159.             await BitmapFactory.DecodeFileAsync(localPath, options);
  160.  
  161.             options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / imageview.Height : options.OutWidth / imageview.Width;
  162.             options.InJustDecodeBounds = false;
  163.  
  164.             Bitmap bitmap = await BitmapFactory.DecodeFileAsync(localPath, options);
  165.             imageview.SetImageBitmap(bitmap);
  166.         }
  167.         private void renewButton()
  168.         {
  169.             this.downloadButton.Click -= cancelDownload;
  170.             this.downloadButton.Click += downloadAsync;
  171.             this.downloadButton.Text = "Download image";
  172.             this.downloadProgress.Progress = 0;
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement