Skip to content

Instantly share code, notes, and snippets.

@jgold6
Last active March 1, 2016 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgold6/56a17fe8ac603e8866aa to your computer and use it in GitHub Desktop.
Save jgold6/56a17fe8ac603e8866aa to your computer and use it in GitHub Desktop.
Copy image in assets to external storage for use in a WebView using local HTML.
using System;
using System.IO;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Android.OS;
using webview_external_resource.Views;
using webview_external_resource.Models;
namespace webview_external_resource
{
[Activity(Label = "webview_external_resource", MainLauncher = true)]
public class MainActivity : Activity
{
public async void SyncAssets()
{
string Content;
string FullFilePath = "";
StreamReader Reader;
StreamWriter Writer;
try
{
FullFilePath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Chrysanthemum.jpg");
if (!File.Exists(FullFilePath))
{
Application.Context.Assets.Open("Chrysanthemum.jpg").CopyTo (File.OpenWrite (FullFilePath));
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Cannot copy Chrysanthemum.jpg to {0} {1}", FullFilePath, ex.ToString()));
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var webView = FindViewById<WebView>(Resource.Id.webView);
webView.Settings.JavaScriptEnabled = true;
// Use subclassed WebViewClient to intercept hybrid native calls
webView.SetWebViewClient(new HybridWebViewClient());
// Render the view from the type generated from RazorView.cshtml
var model = new Model1() { Text = "Text goes here" };
var template = new RazorView() { Model = model };
var page = template.GenerateString();
SyncAssets();
var myhtml = "<html> <Title>this is my html</Title> <body> This is my HTML <img src=\"Chrysanthemum.jpg\" alt=\"Chrysanthemum.jpg\" height=\"55\" width=\"55\" /> </body> </html>";
// NOTE: I am trying to load image from /data/data/app/files/ and it fails
string mypath = System.IO.Path.Combine("file://" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/");
// NOTE: if I use the below to load image from assets then it works.
//string mypath = "file:///android_asset/";
webView.LoadDataWithBaseURL(mypath, myhtml, "text/html", "UTF-8", null);
}
private class HybridWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView webView, string url)
{
// If the URL is not our own custom scheme, just let the webView load the URL as usual
var scheme = "hybrid:";
if (!url.StartsWith(scheme))
return false;
// This handler will treat everything between the protocol and "?"
// as the method name. The querystring has all of the parameters.
var resources = url.Substring(scheme.Length).Split('?');
var method = resources[0];
var parameters = System.Web.HttpUtility.ParseQueryString(resources[1]);
if (method == "UpdateLabel")
{
var textbox = parameters["textbox"];
// Add some text to our string here so that we know something
// happened on the native part of the round trip.
var prepended = string.Format("C# says \"{0}\"", textbox);
// Build some javascript using the C#-modified result
var js = string.Format("SetLabelText('{0}');", prepended);
webView.LoadUrl("javascript:" + js);
}
return true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment