Hi,
We are trying to write a Forms app in VS2013 which downloads documents (txt/pdf/docx) from the internet (azure.blob) and displays the document on the different platforms.
The UI uses Mvvm_light and the pcl code is fine and maps well to the different native platforms.
We use the dependency service feature to invoke platform-specific code for async download/load/save/open files.
For the WP the dependency code is working fine but (not being familiar with mono/droid development) we have problems with the android implementation.
Maybe some of you can help us out.
So basically we want the Android version of the next WP dependency code:
public async Task OpenFile(Uri uri, object optional)
{
var fileName = Path.GetFileName(uri.ToString());
await DownloadFileAsync(uri);
var folder = ApplicationData.Current.LocalFolder;
var sf = await folder.GetFileAsync(fileName);
await Windows.System.Launcher.LaunchFileAsync(sf);
}
private Task<object> DownloadFileAsync(Uri fileUri)
{
var result = new TaskCompletionSource<object>();
var webClient = new WebClient();
webClient.OpenReadCompleted += async (sender, e) =>
{
try
{
var fileName = Path.GetFileName(fileUri.ToString());
var store = IsolatedStorageFile.GetUserStoreForApplication();
using (var isolatedStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
{
using (var sw = new StreamWriter(isolatedStream))
{
await e.Result.CopyToAsync(sw.BaseStream);
}
}
result.SetResult(null);
}
catch (Exception ex)
{
result.TrySetException(ex);
}
};
webClient.OpenReadAsync(fileUri);
return result.Task;
}
For now we have some 'download implementation' on the Android project but using 'DownloadFileAsync' instead of the 'OpenReadAsync'.
And to open the file it we use the next piece of android code which kind of works but isn't async and it looks like when debugging, the files can't be found or aren't stored at all (using a physical device, and choosing a 3rd party viewer for txt/pdf/docx)....
public void openDocument(String pathfilename)
{
if (File.Exists(pathfilename))
{
Java.IO.File targetFile = new Java.IO.File(pathfilename);
Android.Net.Uri targetUri = Android.Net.Uri.FromFile(targetFile);
Intent intent = new Intent(Android.Content.Intent.ActionView);
intent.SetDataAndType(targetUri, "application/*");
var context = global::Xamarin.Forms.Forms.Context;
try
{
context.StartActivity(Intent.CreateChooser(intent, "Choose an Application:"));
}
catch (Exception e)
{ throw e; }
}
}
So any help in translating the WP download code to Android would be appreciated. Or maybe someone can supply us with some links/blogs.
Thanks!
(Ps. We're using the Environment.SpecialFolder.Personal path; not sure if that is quite right....In the projectfile we did set the read/write permissions)