Created
December 24, 2017 15:11
-
-
Save jfversluis/4e9adf60088ff548a1f4e898ba682dfd to your computer and use it in GitHub Desktop.
The LocalFileProvider implementation for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Threading.Tasks; | |
using pdfjs.Droid.PlatformSpecifics; | |
using pdfjs.Interfaces; | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(LocalFileProvider))] | |
namespace pdfjs.Droid.PlatformSpecifics | |
{ | |
public class LocalFileProvider : ILocalFileProvider | |
{ | |
private readonly string _rootDir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "pdfjs"); | |
public async Task<string> SaveFileToDisk(Stream pdfStream, string fileName) | |
{ | |
if (!Directory.Exists(_rootDir)) | |
Directory.CreateDirectory(_rootDir); | |
var filePath = Path.Combine(_rootDir, fileName); | |
using (var memoryStream = new MemoryStream()) | |
{ | |
await pdfStream.CopyToAsync(memoryStream); | |
File.WriteAllBytes(filePath, memoryStream.ToArray()); | |
} | |
return filePath; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment