public void ShareMedia(string text, string path, PurposeColor.Constants.MediaType type) { if (path == null) return; progress.ShowProgressbar( "Preparing share view.." ); string filename = System.IO.Path.GetFileName( path ); Android.Net.Uri uri = Android.Net.Uri.Parse(path); string downloadedFolder = "/storage/emulated/0/Download/"; if( App.DownloadsPath != null && !string.IsNullOrEmpty( App.DownloadsPath ) ) { downloadedFolder = App.DownloadsPath + "/"; } /* if (System.IO.File.Exists(downloadedFolder + filename)) { ShowChooser(type, downloadedFolder + filename, text, path); } else { Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(uri); r.SetDestinationInExternalPublicDir(Android.OS.Environment.ExternalStorageDirectory.ToString(), filename); r.AllowScanningByMediaScanner(); r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyOnlyCompletion); Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService); App.ShareDownloadID = dm.Enqueue(r); }*/ ShowChooser(type, downloadedFolder + filename, text, path); /* MessagingCenter.Subscribe<MyTestReceiver, string>(this, "boom", (page, downpath) => { ShowChooser(type, downpath, text); });*/ }
void ShowChooser(PurposeColor.Constants.MediaType type, string filename, string text, string Uri ) { progress.HideProgressbar(); Intent shareIntent = new Intent(); shareIntent.SetAction(Intent.ActionSend); // shareIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("android.resource://sharetowhtsapp.sharetowhtsapp/" + Resource.Drawable.Icon)); // file:///storage/emulated/0/Download/ + filename string textToShare = ""; text = text + System.Environment.NewLine + System.Environment.NewLine; if (type == Constants.MediaType.Video) { shareIntent.SetType("video/*"); text = System.Environment.NewLine + text + " " + Uri + System.Environment.NewLine; } else if (type == Constants.MediaType.Image) { shareIntent.SetType("image/*"); shareIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse( "file://" + filename)); //text = System.Environment.NewLine + text + " " + Uri + System.Environment.NewLine; } else if (type == Constants.MediaType.Audio) { shareIntent.SetType("audio/*"); text = System.Environment.NewLine + text + " " + Uri + System.Environment.NewLine; } textToShare = text + System.Environment.NewLine + "shared with purpose color." + System.Environment.NewLine + " http:\\www.purposecodes.com"; shareIntent.PutExtra(Intent.ExtraText, textToShare); MessagingCenter.Unsubscribe<MyTestReceiver, string>(this, "boom"); MainActivity.GetMainActivity().StartActivity(Intent.CreateChooser(shareIntent, "Share image via:")); }
async public void AddFileToMediaArray(MemoryStream ms, string path, PurposeColor.Constants.MediaType mediaType) { try { MediaPost mediaWeb = new MediaPost(); string imgType = System.IO.Path.GetExtension(path); string fileName = System.IO.Path.GetFileName(path); MediaItem mediaItem = new MediaItem(); #region MEDIA COMPRESSION AND SERIALISING if (ms != null) { imgType = imgType.Replace(".", ""); if (mediaType == Constants.MediaType.Image) { MemoryStream compressedStream = new MemoryStream(); IResize resize = DependencyService.Get<IResize>(); Byte[] resizedOutput = resize.Resize(ms.ToArray(), (float)(App.screenWidth * App.screenDensity), (float)(App.screenHeight * App.screenDensity), path); if(resizedOutput== null) return; MemoryStream resizedStream = new MemoryStream(resizedOutput); compressedStream = resize.CompessImage(25, resizedStream); var path2 = System.IO.Path.Combine(App.DownloadsPath, fileName); Byte[] inArray = compressedStream.ToArray(); Char[] outArray = new Char[(int)(compressedStream.ToArray().Length * 1.34)]; Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0); string test2 = new string(outArray); App.ExtentionArray.Add(imgType); mediaItem.MediaString = test2; mediaItem.Name = fileName; App.MediaArray.Add(mediaItem); inArray = null; outArray = null; test2 = null; resizedOutput = null; GC.Collect(); } // // imgType = string.Empty; // fileName = string.Empty; } #endregion if(mediaItem != null) { string serviceResult = await PurposeColor.Service.ServiceHelper.SendProfilePicAndStatusNote(string.Empty, mediaItem, imgType); if (serviceResult != null ) { if(callerObject != null) { callerObject.profilePic.Source = Constants.SERVICE_BASE_URL + serviceResult; } User user = App.Settings.GetUser(); if(user != null) { user.ProfileImageUrl = serviceResult; await App.Settings.SaveUser(user); } } } mediaItem = null; if (callerObject != null) { callerObject.progressBar.HideProgressbar(); } } catch (Exception ex) { var test = ex.Message; } }
public void AddFileToMediaArray(MemoryStream ms, string path, PurposeColor.Constants.MediaType mediaType) { try { MediaPost mediaWeb = new MediaPost(); mediaWeb.event_details = string.IsNullOrWhiteSpace(eventDescription.Text) ? string.Empty : eventDescription.Text; mediaWeb.event_title = string.IsNullOrWhiteSpace(eventTitle.Text) ? string.Empty : eventTitle.Text; User user = App.Settings.GetUser(); mediaWeb.user_id = Convert.ToInt32(user.UserId); string imgType = System.IO.Path.GetExtension(path); string fileName = System.IO.Path.GetFileName(path); if (mediaType == Constants.MediaType.Image) { App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("image.png", "image.png", "//Assets//image.png") });//Device.OnPlatform("delete_button.png", "delete_button.png", "//Assets//delete_button.png"); } else if (mediaType == Constants.MediaType.Video) { /*string videoThumpName = Path.GetFileNameWithoutExtension( path ) + ".jpg"; string downloadFilePath = Path.Combine(App.DownloadsPath, videoThumpName); App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform( downloadFilePath, downloadFilePath, "//Assets//video.png") });*/ App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("video.png", "video.png", "//Assets//video.png") }); } else { App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("mic.png", "mic.png", "//Assets//mic.png") }); } #region MEDIA COMPRESSION AND SERIALISING if (ms != null) { imgType = imgType.Replace(".", ""); if (mediaType == Constants.MediaType.Image) { MemoryStream compressedStream = new MemoryStream(); IResize resize = DependencyService.Get<IResize>(); Byte[] resizedOutput = resize.Resize(ms.ToArray(), (float)(App.screenWidth * App.screenDensity), (float)(App.screenHeight *0.50* App.screenDensity), path); MemoryStream resizedStream = new MemoryStream(resizedOutput); int streamLength = (int)resizedStream.Length; int compressionRate = 100; #region compression ratio if (streamLength < 20000) { compressionRate = 100; } else if (streamLength < 40000) { compressionRate = App.screenDensity > 2 ? 100: 95; } else if (streamLength < 50000) { compressionRate = App.screenDensity > 2 ? 100: 91; } else if (streamLength < 100000) { compressionRate = App.screenDensity > 2 ? 100: 90; } else if (streamLength < 200000) { compressionRate = App.screenDensity > 2 ? 99: 89; } else if (streamLength <300000) { compressionRate = App.screenDensity > 2 ? 98: 88; } else if (streamLength < 400000) { compressionRate = App.screenDensity > 2 ? 97: 87; } else if (streamLength < 500000) { compressionRate = App.screenDensity > 2 ? 96: 86; } else if (streamLength < 600000) { compressionRate = App.screenDensity > 2 ? 95: 86; } else if (streamLength < 700000) { compressionRate = App.screenDensity > 2 ? 94: 86; } else if (streamLength < 900000) { compressionRate = App.screenDensity > 2 ? 93: 86; } else if (streamLength < 1000000) { compressionRate = App.screenDensity > 2 ? 92: 86; } else if (streamLength < 2000000) { compressionRate = App.screenDensity > 2 ? 91: 86; } else { compressionRate = App.screenDensity > 2 ? 90: 85; } #endregion compressedStream = resize.CompessImage(compressionRate, resizedStream); Byte[] inArray = resizedStream.ToArray(); Char[] outArray = new Char[(int)(resizedStream.ToArray().Length * 1.34)]; Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0); string test2 = new string(outArray); App.ExtentionArray.Add(imgType); MediaItem item = new MediaItem(); item.MediaString = test2; item.Name = fileName; App.MediaArray.Add(item); inArray = null; outArray = null; test2 = null; item = null; resizedOutput = null; GC.Collect(); } else { Byte[] inArray = ms.ToArray(); Char[] outArray = new Char[(int)(ms.ToArray().Length * 1.34)]; Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0); string test2 = new string(outArray); App.ExtentionArray.Add(imgType); MediaItem item = new MediaItem(); item.MediaString = test2; item.Name = fileName; if( mediaType == Constants.MediaType.Video ) { IVideoCompressor compressor = DependencyService.Get<IVideoCompressor>(); string imgThumbPath = App.DownloadsPath + Path.GetFileNameWithoutExtension( path ) + ".jpg"; MemoryStream thumbStream = compressor.CreateVideoThumbnail( path, imgThumbPath ); Byte[] thumbInArray = thumbStream.ToArray(); Char[] thumbOutArray = new Char[(int)(thumbStream.ToArray().Length * 1.34)]; Convert.ToBase64CharArray(thumbInArray, 0, thumbInArray.Length, thumbOutArray, 0); string thumbString = new string(thumbOutArray); item.MediaType = Constants.MediaType.Video; item.MediaThumbString = thumbString; ms.Dispose(); thumbStream.Dispose(); thumbInArray = null; thumbOutArray = null; } App.MediaArray.Add(item); inArray = null; outArray = null; test2 = null; item = null; GC.Collect(); } imgType = string.Empty; fileName = string.Empty; } #endregion StackLayout preview = (StackLayout)masterLayout.Children.FirstOrDefault(pick => pick.ClassId == "preview"); if (preview != null) { masterLayout.Children.Remove(preview); preview = null; previewListView.ItemsSource = null; previewListView.ItemsSource = App.PreviewListSource; masterLayout.AddChildToLayout(listContainer, 5, 60); } } catch (Exception ex) { DisplayAlert(Constants.ALERT_TITLE, "Unable to add the media", Constants.ALERT_OK); var test = ex.Message; } }