Showing posts with label share. Show all posts
Showing posts with label share. Show all posts

Friday, July 21, 2017

Xamarin Android: How to Share PDF File From Assets Folder? Via Whatsapp I get message, the file you picked was not a document

Leave a Comment

I use Xamarin Android. I have a PDF File storaged in Assest folder from Xamarin Android.

enter image description here

I want to share this file in whatsapp, but I recive a message: "the file you picked was not a document"

enter image description here

I tried two ways:

This is the first way

var SendButton = FindViewById<Button>(Resource.Id.SendButton); SendButton.Click += (s, e) =>                  {                 ////Create a new file in the exteranl storage and copy the file from assets folder to external storage folder                 Java.IO.File dstFile = new Java.IO.File(Environment.ExternalStorageDirectory.Path + "/my-pdf-File--2017.pdf");                 dstFile.CreateNewFile();                  var inputStream = new FileInputStream(Assets.OpenFd("my-pdf-File--2017.pdf").FileDescriptor);                 var outputStream = new FileOutputStream(dstFile);                 CopyFile(inputStream, outputStream);                  //to let system scan the audio file and detect it                 Intent intent = new Intent(Intent.ActionMediaScannerScanFile);                 intent.SetData(Uri.FromFile(dstFile));                 this.SendBroadcast(intent);                  //share the Uri of the file                 var sharingIntent = new Intent();                 sharingIntent.SetAction(Intent.ActionSend);                 sharingIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(dstFile));                 sharingIntent.SetType("application/pdf");                  this.StartActivity(Intent.CreateChooser(sharingIntent, "@string/QuotationShare"));             }; 

This is the second

//Other way              var SendButton2 = FindViewById<Button>(Resource.Id.SendButton2);             SendButton2.Click += (s, e) =>             {                  Intent intent = new Intent(Intent.ActionSend);                 intent.SetType("application/pdf");                  Uri uri = Uri.Parse(Environment.ExternalStorageDirectory.Path + "/my-pdf-File--2017.pdf");                 intent.PutExtra(Intent.ExtraStream, uri);                  try                 {                     StartActivity(Intent.CreateChooser(intent, "Share PDF file"));                 }                 catch (System.Exception ex)                 {                     Toast.MakeText(this, "Error: Cannot open or share created PDF report. " + ex.Message, ToastLength.Short).Show();                 }             }; 

In other way, when I share via email the pdf file is sent empty, "corrupt file"

What can I do?

2 Answers

Answers 1

The solution is copying de .pdf file from assets folder to a local storage. Then We able to share de file.

First copy the file:

string fileName = "my-pdf-File--2017.pdf";  var localFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; var MyFilePath = System.IO.Path.Combine(localFolder, fileName);  using (var streamReader = new StreamReader(Assets.Open(fileName))) {        using (var memstream = new MemoryStream())        {               streamReader.BaseStream.CopyTo(memstream);               var bytes = memstream.ToArray();               //write to local storage               System.IO.File.WriteAllBytes(MyFilePath, bytes);                MyFilePath = $"file://{localFolder}/{fileName}";       } } 

Then share the file, from local storage:

var fileUri = Android.Net.Uri.Parse(MyFilePath);  var intent = new Intent(); intent.SetFlags(ActivityFlags.ClearTop); intent.SetFlags(ActivityFlags.NewTask); intent.SetAction(Intent.ActionSend); intent.SetType("*/*"); intent.PutExtra(Intent.ExtraStream, fileUri); intent.AddFlags(ActivityFlags.GrantReadUriPermission);  var chooserIntent = Intent.CreateChooser(intent, title); chooserIntent.SetFlags(ActivityFlags.ClearTop); chooserIntent.SetFlags(ActivityFlags.NewTask); Android.App.Application.Context.StartActivity(chooserIntent); 

Answers 2

the file you picked was not a document

I had this issue when I trying to share a .pdf file via WhatsApp from assets folder, but it gives me the same error as your question :

the file you picked was not a document 

Finally I got a solution that copy the .pdf file in assets folder to Download folder, it works fine :

var pathFile = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);  Java.IO.File dstFile = new Java.IO.File(pathFile.AbsolutePath + "/my-pdf-File--2017.pdf"); 

Effect like this.

Read More

Sunday, April 17, 2016

share videos to whats app without showing ActionSheet

Leave a Comment

I need to share videos from my app to WhatsApp. I currently can do this by using an UIActivityViewController but the user experience is not good.

(The user presses a "send to WhatsApp" button and then has to select WhatsApp in the Action Sheet displayed by the UIActivityViewController).

I know it's possible to open the WhatsApp application and to share videos. For example, the application TuneMoji does it very well :

  • The user presses the "send to WhatsApp"
  • The WhatsApp application opens, asking for a destination user.

I'd like to do exactly the same.

Please, don't tell me to have a look at https://www.whatsapp.com/faq/en/iphone/23559013 , or to use a UIDocumentInteractionController : I want to avoid presenting an ActionSheet to the user.

4 Answers

Answers 1

Try this on the button action:

func sendVideo(videoName: String, senderView: UIView) {         if UIApplication.sharedApplication().canOpenURL(NSURL(string: "whatsapp://app")!) {         let savePath: String = NSBundle.mainBundle().pathForResource(videoName, ofType: "mov")!        let documentInteractionController = UIDocumentInteractionController(URL: NSURL.fileURLWithPath(savePath))         documentInteractionController.UTI = "net.whatsapp.movie"         documentInteractionController.delegate = self         documentInteractionController.presentOpenInMenuFromRect(CGRectMake(0, 0, 0, 0), inView: senderView, animated: true)     }     else {          let alertController = UIAlertController(title: "Error occured", message: "WhatsApp is not installed on your device.", preferredStyle: .Alert)         let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)         alertController.addAction(defaultAction)         presentViewController(alertController, animated: true, completion: nil)     } } 

For more information you can check ref: https://www.whatsapp.com/faq/en/iphone/23559013

Be sure to include WhatsApp URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of WhatsApp on user's iPhone using 'canOpenURL'

Answers 2

You can open whatsapp like this.

NSURL *urlOfWhatsApp = [NSURL URLWithString:@"whatsapp://"];  if ([[UIApplication sharedApplication] canOpenURL:urlOfWhatsApp]) { //check app can open whatsapp or not.      [[UIApplication sharedApplication] openURL:urlOfWhatsApp];  } else {     NSLog(@"You device do not have whatsapp."); } 

Answers 3

You can send text messages using WhatsApp URL Scheme like this:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"]; if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {     [[UIApplication sharedApplication] openURL: whatsappURL]; } 

This will open the application and ask for destination to send Hello World!.

To send images or videos, you can use the UIActivityViewController like you already noticed, or use UIDocumentInteractionController, that may be closer to what you are looking for:

_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL]; _documentController.delegate = self; _documentController.UTI = @"net.whatsapp.image"; [_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 

To show only whatsapp in the application list, use the private whatsapp file extension or UTI:

Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:

  • images - «.wai» which is of type net.whatsapp.image
  • videos - «.wam» which is of type net.whatsapp.movie
  • audio files - «.waa» which is of type net.whatsapp.audio

When triggered, WhatsApp will immediately present the user with the contact/group picker screen. The media will be automatically sent to a selected contact/group.

More information in this WhatsApp FAQ.

Answers 4

Here you have all you need:

https://www.whatsapp.com/faq/en/iphone/23559013

Edit: and you can see example at this SO to send text / images directly to whats app -

Share image/text through WhatsApp in an iOS app

(see Wagner Sales' answer)

Read More