Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using Android.Graphics;
- using System.IO;
- using System.Net;
- using System.Threading.Tasks;
- namespace Zad2PUM
- {
- [Activity(Label = "Zad2PUM", MainLauncher = true,
- ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
- public class MainActivity : Activity
- {
- int _counter = 0;
- string _imie, _nazwisko;
- WebClient webClient;
- Button downloadButton, clickButton;
- ImageView imageview;
- ProgressBar downloadProgress;
- private EditText Imie, Nazwisko, CounterText, urlText;
- private string urlGlobal;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- if (bundle != null)
- {
- _imie = bundle.GetString("imie", "");
- _nazwisko = bundle.GetString("nazwisko", "");
- _counter = bundle.GetInt("counter", 0);
- }
- CounterText = FindViewById<EditText>(Resource.Id.CounterText);
- Imie = FindViewById<EditText>(Resource.Id.EditName);
- Nazwisko = FindViewById<EditText>(Resource.Id.EditSecondName);
- urlText = FindViewById<EditText>(Resource.Id.ImageAddress);
- this.downloadButton = FindViewById<Button>(Resource.Id.ButtonDowload);
- this.clickButton = FindViewById<Button>(Resource.Id.CounterButton);
- this.imageview = FindViewById<ImageView>(Resource.Id.ImageView);
- this.downloadProgress = FindViewById<ProgressBar>(Resource.Id.ProgressBar);
- CounterText.Text = _counter.ToString();
- urlText.Text = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Reh11ibb.jpg/240px-Reh11ibb.jpg";
- urlGlobal = urlText.Text;
- downloadButton.Click += downloadAsync;
- clickButton.Click += (object sender, EventArgs e) =>
- {
- _counter++;
- CounterText.Text = _counter.ToString();
- };
- }
- async void downloadAsync(object sender, System.EventArgs evarg)
- {
- webClient = new WebClient();
- var url = new Uri(urlGlobal);
- byte[] bytes = null;
- webClient.DownloadProgressChanged += HandleDownloadProgressChanged;
- this.downloadButton.Text = "Stop";
- this.downloadButton.Click -= downloadAsync;
- this.downloadButton.Click += cancelDownload;
- try
- {
- bytes = await webClient.DownloadDataTaskAsync(url);
- }
- catch (TaskCanceledException)
- {
- Toast.MakeText(this, "Przerwano pobieranie", ToastLength.Long).Show();
- return;
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- renewButton();
- return;
- }
- string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
- string localFilename = "sarna.jpg";
- string localPath = System.IO.Path.Combine(documentsPath, localFilename);
- FileStream fs = new FileStream(localPath, FileMode.OpenOrCreate);
- await fs.WriteAsync(bytes, 0, bytes.Length);
- fs.Close();
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.InJustDecodeBounds = true;
- await BitmapFactory.DecodeFileAsync(localPath, options);
- options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / imageview.Height : options.OutWidth / imageview.Width;
- options.InJustDecodeBounds = false;
- Bitmap bitmap = await BitmapFactory.DecodeFileAsync(localPath, options);
- Toast.MakeText(this, "Pobrano!", ToastLength.Long).Show();
- imageview.SetImageBitmap(bitmap);
- renewButton();
- }
- void HandleDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- this.downloadProgress.Progress = e.ProgressPercentage;
- }
- void cancelDownload(object sender, System.EventArgs eeaa)
- {
- Toast.MakeText(this, "Przerwano pobieranie", ToastLength.Long).Show();
- if (webClient != null)
- webClient.CancelAsync();
- webClient.DownloadProgressChanged -= HandleDownloadProgressChanged;
- renewButton();
- }
- protected override void OnSaveInstanceState(Bundle outState)
- {
- outState.PutInt("counter", _counter);
- outState.PutString("imie", _imie);
- outState.PutString("nazwisko", _nazwisko);
- base.OnSaveInstanceState(outState);
- }
- protected override void OnRestoreInstanceState(Bundle savedState)
- {
- base.OnRestoreInstanceState(savedState);
- _counter = savedState.GetInt("counter");
- _imie = savedState.GetString("imie");
- _nazwisko = savedState.GetString("nazwisko");
- }
- private async Task loadMyImage()
- {
- string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
- string localFilename = "sarna.jpg";
- string localPath = System.IO.Path.Combine(documentsPath, localFilename);
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.InJustDecodeBounds = true;
- await BitmapFactory.DecodeFileAsync(localPath, options);
- options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / imageview.Height : options.OutWidth / imageview.Width;
- options.InJustDecodeBounds = false;
- Bitmap bitmap = await BitmapFactory.DecodeFileAsync(localPath, options);
- imageview.SetImageBitmap(bitmap);
- }
- private void renewButton()
- {
- this.downloadButton.Click -= cancelDownload;
- this.downloadButton.Click += downloadAsync;
- this.downloadButton.Text = "Download image";
- this.downloadProgress.Progress = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement