Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

File.WriteAllBytes in Android API 21+

$
0
0

Recently I wrote up a bit of basic code for our XF project for Android as an alternative to our existing iOS functionality to open/display PDF files.

Basically it just fetches bytes from a web service, writes a file to the file system, and then opens the file. This was developed and tested on an Android 4.4.1 device, and works fine still, but attempting to use this with a Lollipop device (5.1.1 specifically), it just seems to break out of the current async Task, suggesting some exception, and I can't pinpoint what the problem is.

From what I can guess the application is really just having an issue writing to the directory. Not sure if maybe this directory is invalid in Lollipop, or it's a permissions issue (External storage)? Code snippet below:

public async Task WriteBytesAsync(string filename, byte[] bytes, string folder)
{
    string file = GetFilePath(filename, folder);
    File.WriteAllBytes(file, bytes);
}

string GetFilePath(string filename, string folder)
{
    return Path.Combine(GetDocsFolder(), filename);
}

string GetDocsFolder()
{
    string folder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).Path;
    return folder;
}

Any ideas on what the cause of this could be? Thanks.


Viewing all articles
Browse latest Browse all 91519

Trending Articles