First, make sure that in the Manifest file you enable WriteExternalStorage
To read or write files to external storage, your application must have READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission. For example:
<manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
Constructor
FileStream (one of many) takes a string for the path and FileMode , so there will be an approximate solution.
var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var path = Path.Combine(directory, "myTestFile.pdf"); var fs = new FileStream(path, FileMode.Create);
This creates a folder called "pdf" for external storage, and then creates a pdf file. Additional code can be included to check if the file exists before it is created.
Edit: full example just tested on my device:
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var directory = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory, "pdf").ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var path = Path.Combine(directory, "myTestFile.pdf"); if (File.Exists(path)) { File.Delete(path); } var fs = new FileStream(path, FileMode.Create); Document document = new Document(PageSize.A4, 25, 25, 30, 30); PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); document.Add(new Paragraph("Hello World")); document.Close(); writer.Close(); fs.Close(); Java.IO.File file = new Java.IO.File(path); Intent intent = new Intent(Intent.ActionView); intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf"); StartActivity(intent); }
Milen
source share