Forum Xamarin.Android

a local html file

RuiXRuiX PTMember ✭✭

Hi
I am using
http://developer.xamarin.com/recipes/android/controls/webview/load_local_content/
and
setting the file primeira.html as content as we should at
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/files/

but the requested file is never found...
I use this path
localWebView.LoadUrl("file:///android_asset/Content/primeira.html");

Can you help?

Posts

  • Take a look here, almost the same question: Extract HTML source code

  • RuiXRuiX PTMember ✭✭

    Thanks but what is the reference I should add at the start of the file in order to be able to use
    Android.Net.Uri uri = Android.Net.Uri.Parse(Path.Combine(Application.Context.FilesDir.Path, "subfolderA/subfolderAA", document.FileName));

  • CheesebaronCheesebaron DKInsider, University mod

    Why do you need to do that? Please elaborate.

  • RuiXRuiX PTMember ✭✭

    I´m using
    localWebView.LoadUrl("file:///android_asset/Content/primeira.html");

    but I ant to use this... with Path.Combine, because I think that Path gets the local path until the file...
    //string fileName = "Content/Home.html"; // remember case-sensitive
    //string localHtmlUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
    //webView.LoadRequest(new NSUrlRequest(new NSUrl(localHtmlUrl, false)));

    nevertheless my app doesn't recognize Path or LoadRequest

    Thanks

  • CheesebaronCheesebaron DKInsider, University mod

    NSBundle? NSUrl? This is not iOS...

  • RuiXRuiX PTMember ✭✭

    Thanks. I understand and forget that code...
    But having "primeira.html" inside Resources, why Android doesn't find it with
    localWebView.LoadUrl("file:///android_asset/Content/primeira.html");

    ?

    How can I write this path?

  • RuiXRuiX PTMember ✭✭

    I want a path for internal storage... also I forget this "android_asset"
    I create the html file inside the app

  • CheesebaronCheesebaron DKInsider, University mod

    You need to put your stuff inside of assets, otherwise you won't be able to read it.

  • xceedxceed GBMember ✭✭

    Would you need to set the build action to AndroidAsset?

  • RuiXRuiX PTMember ✭✭

    I will make some tests with your information.
    Then I will tell you what I was able to do.
    I send my screen attached.
    Thanks

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Depending on the size of your "primeira.html" you can copy the markup and store it inside a string.
    Doing so you can load your web-page like this:

        webView.LoadDataWithBaseURL("blub://yippi", "YOUR_STRING_HERE", null, "UTF-8", "");
    
  • RuiXRuiX PTMember ✭✭

    Thanks to all of you, the problem was solved!

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭
    edited October 2014

    Glad you solved the problem. Are you willing to share the solution for a later reference if someone else can look it up here in the forum

  • RuiXRuiX PTMember ✭✭

    I used
    localWebView.LoadUrl("file:///android_asset/primeira.html");
    with the file inside assets and with the build action (right click the file) set to AndroidAsset

    All best

  • AlperSilistreAlperSilistre USMember

    I face with same issue and thanks for this topic. Xamarin documentation about loading html in to the WebView is wrong. It says put localWebView.LoadUrl("file:///android_asset/Content/Home.html"); but it shouldn't be Content. I am calling with localWebView.LoadUrl("file:///android_asset/Home.html");

  • JohnHoJohnHo USMember ✭✭

    I try to do the same thing in Share project with Xamarin form but it does not work(It works in Android project)
    what do I do wrong?

    public class helpPage : ContentPage
    {
    public helpPage ()
    {

            var source = new HtmlWebViewSource();
            //source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
            //var assetManager = Xamarin.Forms.Forms.Context.Assets;
            //using (var streamReader = new StreamReader(assetManager.Open("local.html")))
           //{
             //   source.Html = streamReader.ReadToEnd();
            //}
    
            source.Html = @"<html><body>
                  <h1>Xamarin.Forms</h1>
                  <p>Welcome to WebView.</p>
                  </body></html>";
    
            var _webView = new WebView
            {
                Source = source,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };
        }
    

    }

Sign In or Register to comment.