Extract the MIME type from a base64 string:
/**
* Extract the MIME type from a base64 string
* @param encoded Base64 string
* @return MIME type string
*/
private static String extractMimeType(final String encoded) {
final Pattern mime = Pattern.compile("^data:([a-zA-Z0-9]+/[a-zA-Z0-9]+).*,.*");
final Matcher matcher = mime.matcher(encoded);
if (!matcher.find())
return "";
return matcher.group(1).toLowerCase();
}
Usage:
final String encoded = "data:image/png;base64,iVBORw0KGgoAA...5CYII=";
extractMimeType(encoded); // "image/png"
extractMimeType("garbage"); // ""
Then you can write your byte array:
var filePath = System.IO.Path.Combine(folderPath, string.Format("pdf_{0}.pdf", Guid.NewGuid()));
var byteArray = Convert.FromBase64String(base64pdf);
File.WriteAllBytes(filePath, byteArray);
And open you file:
Device.OpenUri(new Uri("file://" + filePath));
Or tokenize the data since the 64 encoded data look like this "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAKAC
" and parse that string.