private static void Upload(SmartPrintScreen service, string screenshotFile) { var permissionCheck = ContextCompat.CheckSelfPermission(service, Android.Manifest.Permission.ReadExternalStorage); if (permissionCheck != Android.Content.PM.Permission.Granted) { SmartPrintScreen.MakeToast(service, Strings.RequestRead); return; } using (var opt = new BitmapFactory.Options()) { opt.InDither = true; opt.InPreferredConfig = Bitmap.Config.Argb8888; var bitmap = BitmapFactory.DecodeFile(screenshotFile, opt); if (bitmap != null) { bool disposed = false; Task.Run(async() => { string url = await SmartPrintScreen.UploadToImgurTask(bitmap); bitmap.Dispose(); disposed = true; SmartPrintScreen.PostUpload(service, url, screenshotFile); }).ContinueWith((t) => { if (!disposed) { bitmap.Dispose(); } service.handler.Post(() => { ExceptionHandler.SaveManagedException(Java.Lang.Throwable.FromException(t.Exception), Java.Lang.Thread.CurrentThread(), null); }); }, TaskContinuationOptions.OnlyOnFaulted); } } }
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { switch (requestCode) { case 1337: if (grantResults[0] != Permission.Granted || grantResults[1] != Permission.Granted) { SmartPrintScreen.MakeToast(this, Strings.RequestRead); } break; } }
private static void PostUpload(SmartPrintScreen service, string url, string screenshotFile) { Log.Debug(Tag, "SmartPrintScreen: PostUpload " + url); if (url != null) { Log.Debug(Tag, "SmartPrintScreen: PostUpload prepared showing toast"); // Device.BeginInvokeOnMainThread(() => { service.handler.Post(() => { Log.Debug(Tag, "SmartPrintScreen: PostUpload got from the clipboard"); Plugin.Clipboard.CrossClipboard.Current.SetText(url); SmartPrintScreen.MakeToast(service, Strings.CopiedToClipboardToast + ":\n" + url); MessagingCenter.Send(url, MainPage.AddedURL); }); // }); //we reached here, so it got uploaded fine if (Settings.RemoveShots) { Log.Debug(Tag, "SmartPrintScreen: PostUpload removing the screenshot"); Java.IO.File f = new Java.IO.File(screenshotFile); Log.Debug(Tag, "SmartPrintScreen: PostUpload removing the screenshot from " + f.ToString()); if (f.Exists() && !f.IsDirectory) { if ( f.Delete() ) { Log.Debug(Tag, "SmartPrintScreen: PostUpload removed the screenshot"); } else { Log.Debug(Tag, "SmartPrintScreen: PostUpload failed to removed the screenshot"); } } } else { Log.Debug(Tag, "SmartPrintScreen: PostUpload not removing the screenshot"); } Log.Debug(Tag, "SmartPrintScreen: PostUpload updating the screenshots list"); Settings.ScreenshotsList += url + "\n"; Log.Debug(Tag, "SmartPrintScreen: PostUpload notifying the UI"); //if wi-fi is enabled then we actually failed } else { service.handler.Post(() => { SmartPrintScreen.MakeToast(service, Strings.AppName + ": " + Strings.ErrorCheckConnection); }); } Log.Debug(Tag, "SmartPrintScreen: PostUpload finished"); }