2017-07-12 84 views
6

Używam Xamarin Android. Mam plik PDF przechowywany w folderze Assets z Xamarin Android.Xamarin Android: jak udostępnić plik PDF z folderu zasobów? Przez WhatsApp otrzymuję wiadomość, że wybrany plik nie był dokumentem

enter image description here

Chcę podzielić ten plik w WhatsApp, ale pojawia się komunikat:

Plik, który wybrał nie był dokument.

enter image description here

Próbowałem na dwa sposoby:

To pierwszy sposób

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")); 
      }; 

Jest to drugi

//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(); 
       } 
      }; 

inaczej, gdy dzielę się za pośrednictwem poczty elektronicznej, plik PDF zostanie wysłany pusty (uszkodzony plik)

, co mogę zrobić?

+0

nie muszę przykłady czy coś takiego, chyba że jest rozwiązać mój konkretny przypadek. – JotaPardo

Odpowiedz

3

Rozwiązaniem jest skopiowanie pliku de .pdf z folderu assets do local storage. Następnie możemy udostępnić plik de.

Pierwszy skopiować plik:

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}"; 

    } 
} 

Następnie udostępnić plik, z lokalnej pamięci masowej:

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); 
+0

to jest fajne ... – GvSharma

1

plik, który wybrał nie była dokumentem

miałem ten problem, kiedy próbuje udostępnić plik .pdf poprzez WhatsApp z folderu aktywów, ale to daje mi ten sam błąd jak pytanie:

the file you picked was not a document 

Wreszcie mam rozwiązanie skopiować plik w folderze aktywów .pdf do Pobierz folderze, to działa dobrze:

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"); 

Efekt podobny do this.

+0

Jak skopiować plik .pdf do folderu Pobierz z folderu zasobów? – JotaPardo