Exemplo n.º 1
0
        async public static void Share(ImageSource imageSource)
        {
            try
            {
                var handler = new ImageLoaderSourceHandler();
                var uiImage = await handler.LoadImageAsync(imageSource);

                var item               = NSObject.FromObject(uiImage);
                var item1              = NSObject.FromObject("Fantastic Bike from Bikspot.");
                var activityItems      = new[] { item, item1 };
                var activityController = new UIActivityViewController(activityItems, null);

                var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

                while (topController.PresentedViewController != null)
                {
                    topController = topController.PresentedViewController;
                }


                if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
                {
                    activityController.PopoverPresentationController.SourceView = topController.View;
                    activityController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((topController.View.Bounds.Width / 2), (topController.View.Bounds.Height / 4), 0, 0);
                }


                topController.PresentViewController(activityController, true, () => { });
                //topController.PopoverPresentationController(activityController, true, () => { });
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 2
0
        public async void Share(string imageUrl)
        {
            var intent = new Intent(Intent.ActionSend);

            intent.SetType("image/png");

            // Get the URI of the image
            var imageUri    = new Uri(imageUrl);
            var imageSource = ImageSource.FromUri(imageUri);
            var handler     = new ImageLoaderSourceHandler();
            var context     = Xamarin.Forms.Forms.Context;
            var bitmap      = await handler.LoadImageAsync(imageSource, context);

            // Get the file name
            var filename = System.IO.Path.GetFileName(imageUrl);
            var path     =
                Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads +
                                                                         Java.IO.File.Separator + filename);

            using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            }


            intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));

            var intentChooser = Intent.CreateChooser(intent, "Share via");

            context.StartActivity(intentChooser);
        }
Exemplo n.º 3
0
        public async void Share(string subject, string message, ImageSource image)
        {
            //await CheckAppPermissions();
            try
            {
                var intent = new Intent(Intent.ActionSend);
                //intent.PutExtra(Intent.ExtraSubject, subject);
                intent.PutExtra(Intent.ExtraText, message);
                intent.SetType("image/png");

                var handler = new ImageLoaderSourceHandler();
                var bitmap  = await handler.LoadImageAsync(image, this);

                /*
                 * var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads + Java.IO.File.Separator + "logo.png");
                 *
                 * using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
                 * {
                 *  bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
                 * }
                 * intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
                 * Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
                 *
                 */


                /*
                 * string documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
                 * string folderName = "CurrentApp";
                 * var folderPath = System.IO.Path.Combine(documentPath, folderName);
                 * System.IO.Directory.CreateDirectory(folderPath);
                 * string fName = "Download.pdf";
                 * var localPath = System.IO.Path.Combine(folderPath, fName);
                 * using (var os = new System.IO.FileStream(localPath, System.IO.FileMode.Create))
                 * {
                 *  bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
                 * }
                 * intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile((Java.IO.File)localPath));
                 * Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
                 */


                //var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads + Java.IO.File.Separator + "logo.png");

                var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads + Java.IO.File.Separator + "logo.png" + System.DateTime.Now.Ticks.ToString());

                using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
                {
                    bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
                }
                intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
                Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
            }
            catch (System.Exception ex)
            {
                PrintLog.PublishLog(ex);
            }
        }
Exemplo n.º 4
0
        async public Task <System.Windows.Media.ImageSource> LoadImageAsync(ImageSource imageSource, CancellationToken cancelationToken)
        {
            System.Windows.Media.ImageSource image = await _imageLoaderSourceHandler.LoadImageAsync(imageSource, cancelationToken);

            EventHandler <bool> statusChangedHandler = (EventHandler <bool>)ImageSourceExtensions.GetStatusChangedHandler(imageSource);

            if (statusChangedHandler != null)
            {
                statusChangedHandler(imageSource, image != null);
            }
            return(image);
        }
        async public Task <UIImage> LoadImageAsync(ImageSource imageSource, CancellationToken cancelationToken, float scale)
        {
            UIImage image = await _imageLoaderSourceHandler.LoadImageAsync(imageSource, cancelationToken, scale);

            EventHandler <bool> statusChangedHandler = (EventHandler <bool>)ImageSourceExtensions.GetStatusChangedHandler(imageSource);

            if (statusChangedHandler != null)
            {
                statusChangedHandler(imageSource, image != null);
            }
            return(image);
        }
Exemplo n.º 6
0
        public async Task <Bitmap> LoadImageAsync(ImageSource imageSource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            Bitmap bitmap = await _imageLoaderSourceHandler.LoadImageAsync(imageSource, context, cancelationToken);

            EventHandler <bool> statusChangedHandler = (EventHandler <bool>)ImageSourceExtensions.GetStatusChangedHandler(imageSource);

            if (statusChangedHandler != null)
            {
                statusChangedHandler(imageSource, bitmap != null);
            }
            return(bitmap);
        }
Exemplo n.º 7
0
        async void Share (ImageSource imageSource)
        {
            var handler = new ImageLoaderSourceHandler();
            var uiImage = await handler.LoadImageAsync(imageSource);

            var item = NSObject.FromObject (uiImage);
            var activityItems = new[] { item }; 
            var activityController = new UIActivityViewController (activityItems, null);

            var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (topController.PresentedViewController != null) {
                topController = topController.PresentedViewController;
            }

            topController.PresentViewController (activityController, true, () => {});
        }
Exemplo n.º 8
0
        public async void Share(string subject, string message, ImageSource image)
        {
            var handler = new ImageLoaderSourceHandler();
            var uiImage = await handler.LoadImageAsync(image);

            var img                = NSObject.FromObject(uiImage);
            var mess               = NSObject.FromObject(message);
            var activityItems      = new[] { mess, img };
            var activityController = new UIActivityViewController(activityItems, null);
            var topController      = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (topController.PresentedViewController != null)
            {
                topController = topController.PresentedViewController;
            }

            topController.PresentViewController(activityController, true, () => { });
        }
Exemplo n.º 9
0
        public async void Share(string imageUrl)
        {
            // Get the URI of the image
            var imageUri    = new Uri(imageUrl);
            var imageSource = ImageSource.FromUri(imageUri);
            var handler     = new ImageLoaderSourceHandler();
            var uiImage     = await handler.LoadImageAsync(imageSource);

            var item               = NSObject.FromObject(uiImage);
            var activityItems      = new[] { item };
            var activityController = new UIActivityViewController(activityItems, null);

            var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (topController.PresentedViewController != null)
            {
                topController = topController.PresentedViewController;
            }

            topController.PresentViewController(activityController, true, () => { });
        }
Exemplo n.º 10
0
        async void Share(ImageSource imageSource)
        {
            var intent = new Intent(Intent.ActionSend);

            intent.SetType("image/png");

            var handler = new ImageLoaderSourceHandler();
            var bitmap  = await handler.LoadImageAsync(imageSource, this);

            var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads
                                                                     + Java.IO.File.Separator + "logo.png");

            using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create)) {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            }

            intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));

            var intentChooser = Intent.CreateChooser(intent, "Share via");

            StartActivityForResult(intentChooser, ShareImageId);
        }
Exemplo n.º 11
0
        public async void Share(string subject, string message, ImageSource imageSource)
        {
            var intent = new Intent(Intent.ActionSend);

            //intent.PutExtra(Intent.ExtraSubject, subject);
            intent.PutExtra(Intent.ExtraText, message);
            intent.SetType("image/png");

            var handler = new ImageLoaderSourceHandler();
            var bitmap  = await handler.LoadImageAsync(imageSource, this);

            var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures +
                                                                                Java.IO.File.Separator + "player.png");

            using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            }

            intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
            MainApplication.CurrentContext.StartActivity(Intent.CreateChooser(intent, "Share Image"));
        }
Exemplo n.º 12
0
        public async Task <Bitmap> LoadImageAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = new CancellationToken())
        {
            var uriImageSource = imagesource as UriImageSource;

            if ((uriImageSource != null) && (uriImageSource.Uri != null))
            {
                var key = uriImageSource.Uri.ToString();

                TaskCompletionSource <object> existingTask = null;

                lock (lockObj)
                    currentCalls.TryGetValue(key, out existingTask);

                if (existingTask != null)
                {
                    await existingTask.Task;
                }

                var task = new TaskCompletionSource <object>();

                lock (lockObj)
                    currentCalls[key] = task;

                var uiImage = await originalImageLoaderSourceHandler.LoadImageAsync(imagesource, Forms.Context, cancelationToken);

                lock (lockObj)
                    if (currentCalls.TryGetValue(key, out existingTask) && (existingTask == task))
                    {
                        currentCalls.Remove(key);
                    }

                task.SetResult(null);

                return(uiImage);
            }

            return(null);
        }
Exemplo n.º 13
0
 public static Task <UIImage> ToUIImage(this ImageSource imageSource)
 {
     return(s_imageLoaderSourceHandler.LoadImageAsync(imageSource));
 }
Exemplo n.º 14
0
        /// <summary>
        /// Share a message with compatible services
        /// </summary>
        /// <param name="message">Message to share</param>
        /// <param name="options">Platform specific options</param>
        /// <returns>True if the operation was successful, false otherwise</returns>
        public async Task <bool> Share(ShareMessage message, ShareOptions options = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                var items = new List <string>();
                if (message.Text != null)
                {
                    items.Add(message.Text);
                }
                if (message.Url != null)
                {
                    items.Add(message.Url);
                }

                var intent = new Intent(Intent.ActionSend);

                intent.PutExtra(Intent.ExtraText, string.Join(Environment.NewLine, items));

                if (message.Title != null)
                {
                    intent.PutExtra(Intent.ExtraSubject, message.Title);
                }

                if (message.Image != null)
                {
                    var handler = new ImageLoaderSourceHandler();
                    var bitmap  = await handler.LoadImageAsync(message.Image, Android.App.Application.Context);

                    var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads
                                                                                        + Java.IO.File.Separator + Guid.NewGuid().ToString() + ".png");

                    using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
                    {
                        bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
                    }

                    intent.SetType("image/*");
                    intent.SetFlags(ActivityFlags.GrantReadUriPermission);
                    intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
                }
                else
                {
                    intent.SetType("text/plain");
                }

                var chooserIntent = Intent.CreateChooser(intent, options?.ChooserTitle);
                chooserIntent.SetFlags(ActivityFlags.ClearTop);
                chooserIntent.SetFlags(ActivityFlags.NewTask);
                Application.Context.StartActivity(chooserIntent);

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to share: " + ex.Message);
                return(false);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Share a message with compatible services
        /// </summary>
        /// <param name="message">Message to share</param>
        /// <param name="options">Platform specific options</param>
        /// <param name="excludedActivityTypes">UIActivityTypes that should not be displayed</param>
        /// <returns>True if the operation was successful, false otherwise</returns>
        private async Task <bool> Share(ShareMessage message, ShareOptions options = null, params NSString[] excludedActivityTypes)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                // create activity items
                var items = new List <NSObject>();
                if (message.Text != null)
                {
                    items.Add(new ShareActivityItemSource(new NSString(message.Text), message.Title));
                }
                if (message.Url != null)
                {
                    items.Add(new ShareActivityItemSource(NSUrl.FromString(message.Url), message.Title));
                }
                if (message.Image != null)
                {
                    var handler = new ImageLoaderSourceHandler();

                    var uiImage = await handler.LoadImageAsync(message.Image);

                    items.Add(NSObject.FromObject(uiImage));
                }

                // create activity controller
                var activityController = new UIActivityViewController(items.ToArray(), null);

                // set excluded activity types
                if (excludedActivityTypes == null)
                {
                    // use ShareOptions.ExcludedUIActivityTypes
                    excludedActivityTypes = options?.ExcludedUIActivityTypes?.Select(x => GetUIActivityType(x)).Where(x => x != null).ToArray();
                }

                if (excludedActivityTypes == null)
                {
                    // use ShareImplementation.ExcludedUIActivityTypes
                    excludedActivityTypes = ExcludedUIActivityTypes?.ToArray();
                }

                if (excludedActivityTypes != null && excludedActivityTypes.Length > 0)
                {
                    activityController.ExcludedActivityTypes = excludedActivityTypes;
                }

                // show activity controller
                var vc = GetVisibleViewController();

                if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    if (activityController.PopoverPresentationController != null)
                    {
                        activityController.PopoverPresentationController.SourceView = vc.View;

                        var rect = options?.PopoverAnchorRectangle;
                        if (rect != null)
                        {
                            activityController.PopoverPresentationController.SourceRect = new CGRect(rect.X, rect.Y, rect.Width, rect.Height);
                        }
                    }
                }

                await vc.PresentViewControllerAsync(activityController, true);

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to share: " + ex.Message);
                return(false);
            }
        }