Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Video and Image Sharing on Cross Platform

Patil2421Patil2421 USMember ✭✭✭
edited March 2017 in Xamarin.Forms

Was looking for sharing video and images on other social networking or sharing apps like facebook, gmail, shareIt etc.
Here is what i've done using the default device sharing option for my media and you can share it on all available apps on device and sharing list.
Done this using Messaging center, you need to have exact path of your video to be shared.
This is the sample of Video sharing for Image you just need to change the intent.SetType("image/*") for Droid Platform.

Here is what you have to do:

In PCL:

    btnShare.Clicked += (sender, e) =>
  
    {           
        MessagingCenter.Send<string>(video.video_url, "Share");

    } ;


In Droid, In your MainActivity.cs file add this method:

    void Share(string fileName)
        
    {
          
    var intent = new Intent(Intent.ActionSend);
            
    intent.SetType("video/*");
         
    var path = "file://" + Environment.ExternalStorageDirectory.AbsolutePath + "/" + "Foldername" + "/" + fileName;
            intent.PutExtra(Intent.ExtraStream, Uri.Parse(path));
          
    var intentChooser = Intent.CreateChooser(intent, "Share via");
         
    StartActivityForResult(intentChooser, ShareImageId);
       
    }


In iOS, Add this your AppDelegate.cs file:

    void Share(string fileName)
        
    {
          
    var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
          
    var directoryname = Path.Combine(documents, "FolderName" + "/" + fileName);
            
    var ii = NSUrl.FromFilename(directoryname);
            
    var item = ii.Copy();
          
    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, () => { });
      
    }



Posts

Sign In or Register to comment.