2017-07-12 24 views
6

मैं ज़ैमरिन एंड्रॉइड का उपयोग करता हूं। मेरे पास X12arin एंड्रॉइड से संपत्ति फ़ोल्डर में संग्रहीत एक पीडीएफ फ़ाइल है।एक्समरिन एंड्रॉइड: संपत्ति फ़ोल्डर से पीडीएफ फाइल कैसे साझा करें? व्हाट्सएप के माध्यम से मुझे संदेश मिलता है कि आपके द्वारा चुनी गई फ़ाइल दस्तावेज़ नहीं थी

enter image description here

मैं WhatsApp में इस फ़ाइल को साझा करना चाहते हैं, लेकिन मैं संदेश प्राप्त होता है:

फ़ाइल आपके द्वारा चुने गए एक दस्तावेज नहीं था।

enter image description here

मैं दो तरीकों की कोशिश की:

यह पहला रास्ता

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

यह दूसरा

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

अन्य तरह से, जब मैं ईमेल के माध्यम से साझा करते हैं, पीडीएफ फाइल खाली (भ्रष्ट फ़ाइल)

मैं क्या कर सकता भेज दिया जाता है?

+0

मैं, ऐसा उदाहरण या कुछ और की जरूरत नहीं है जब तक यह करने के लिए है मेरे विशेष मामले को हल करें। – JotaPardo

उत्तर

3

समाधान .pdf फ़ाइल assets फ़ोल्डर से local storage पर कॉपी कर रहा है। फिर हम डी फाइल साझा करने में सक्षम हैं।

पहले फ़ाइल कॉपी:

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

    } 
} 

फिर शेयर फ़ाइल, स्थानीय भंडारण से:

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

यह अच्छा है ... – GvSharma

1

फ़ाइल आपके द्वारा चुने गए एक दस्तावेज

मैं इस मुद्दे को जब मैं संपत्ति फ़ोल्डर से WhatsApp के माध्यम से एक .pdf फ़ाइल साझा करने के लिए कोशिश कर रहा था नहीं था, लेकिन यह मेरे अपने प्रश्न के रूप में ही त्रुटि देता है:

the file you picked was not a document 

अंत में मैं एक समाधान है कि करने के लिए डाउनलोड फ़ोल्डर संपत्ति फ़ोल्डर में .pdf फ़ाइल की प्रतिलिपि है, यह ठीक काम करता है:

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

प्रभाव this जैसा प्रभाव।

+0

मैं संपत्ति फ़ोल्डर से फ़ोल्डर डाउनलोड करने के लिए .pdf फ़ाइल को कैसे कॉपी कर सकता हूं? – JotaPardo

संबंधित मुद्दे